spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php_forms2 / 1 To page 1To page 2To page 3current page
[previous]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


How to Interact with Web Forms.. Part 2

Getting Information About File Uploads

When uploading files to the web server using <input type=”file” />, the HTML form has to fulfill two requirements:

  • The enctype attribute has to be set to ”multipart/form-data”

  • The method attribute has to be set to ”post”

Without these settings, the file upload does not work. It also does not work if the following information is missing from php.ini:

file_uploads = On

But if it does, retrieving information about the file is quite easy: In the (superglobal) array $_FILES, you can find the file upload form field under its name.Then, the following subkeys provide further information about the uploaded file:

  • $_FILES[”File”][”error”]—Error code (0 in case of success)

  • $_FILES[”File”][”name”]—Original filename

  • $_FILES[”File”][”size”]—Size of the file

  • $_FILES[”File”][”tmp_name”]—Temporary filename where PHP saved the file

  • $_FILES[”File”][”type”]—The file’s MIME type as sent by the client, not reliable

The preceding code outputs the available file information (see Figure 4.4).

Moving Uploaded Files to a Safe Location

When a user uploads a file to a PHP script using the <input type=”file” /> HTML element, PHP stores the file in a temporary location (set in the php.ini directive upload_tmp_dir) and deletes it upon completion of script execution.Therefore, you have to access the uploaded file within the script.To do so, PHP contains the function move_uploaded_file(), which moves a file from one location to another.The great thing about move_uploaded_file() is that the function first does a sanity check, whether the filename you provide really is an uploaded file or if a malicious user just tried to trick you into moving /etc/passwd or C:\boot.ini somewhere else.

Suppose the path /tmp exists and is writable by the web server and the PHP process.Then, the preceding code moves the uploaded file to this directory, using its original filename (and you do not care whether the filename already exists).

Excerpted from Chapter 4: Interacting with Web Forms in the PHP Phrasebook by Christian Wenz. ISBN 0672328178, Copyright © 2005. Used with the permission of Sams Publishing.

home / programming / php_forms2 / 1 To page 1To page 2To page 3current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27, 2003
Revised: January 23, 2006

URL: http://webreference.com/programming/php_forms2/1