| home / programming / php_forms2 / 1 | [previous] |
|
|

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

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



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 | [previous] |
Created: March 27, 2003
Revised: January 23, 2006
URL: http://webreference.com/programming/php_forms2/1