Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://old.master.cmc.msu.ru/php/features.file-upload.html
Дата изменения: Sun Feb 3 22:33:44 2002 Дата индексирования: Tue Oct 2 04:50:06 2012 Кодировка: |
PHP is capable of receiving file uploads from any RFC-1867 compliant browser (which includes Netscape Navigator 3 or later, Microsoft Internet Explorer 3 with a patch from Microsoft, or later without a patch). This feature lets people upload both text and binary files. With PHP's authentication and file manipulation functions, you have full control over who is allowed to upload and what is to be done with the file once it has been uploaded.
Note that PHP also supports PUT-method file uploads as used by Netscape Composer and W3C's Amaya clients. See the PUT Method Support for more details.
A file upload screen can be built by creating a special form which looks something like this:
Warning |
The MAX_FILE_SIZE is advisory to the browser. It is easy to circumvent this maximum. So don't count on it that the browser obeys you wish! The PHP-settings for maximum-size, however, cannot be fooled. |
Variables defined for uploaded files differs depends on PHP version and configuration. Following variables will be defined within the destination script upon a successful upload. When track_vars is enabled, $HTTP_POST_FILES/ $_FILES array is initialized. track_vars is always on from PHP 4.0.3. Finally, related variables may be initialized as globals when register_globals is turned on . However, use of globals is not recommended anymore.
Note: track_vars is always on from PHP 4.0.3. From PHP 4.1.0 or later, $_FILES may be used instead of $HTTP_POST_FILES. $_FILES is always global, so global is should not be used for $_FILES in function scope.
$HTTP_POST_FILES/$_FILES is provided to contain the uploaded file information.
The contents of $HTTP_POST_FILES are as follows. Note that this assumes the use of the file upload name 'userfile', as used in the example script above:
The original name of the file on the client machine.
The mime type of the file, if the browser provided this information. An example would be "image/gif".
The size, in bytes, of the uploaded file.
The temporary filename of the file in which the uploaded file was stored on the server.
Note: PHP 4.1.0 or later supports short track var variable $_FILES. PHP3 does not support $HTTP_POST_FILES.
When register_globals is turned on in php.ini. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example script above:
$userfile - The temporary filename in which the uploaded file was stored on the server machine.
$userfile_name - The original name or path of the file on the sender's system.
$userfile_size - The size of the uploaded file in bytes.
$userfile_type - The mime type of the file if the browser provided this information. An example would be "image/gif".
Note: register_globals = On is not recommended for security and performance reason.
Files will by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server's default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well.
Example 19-2. Validating file uploads The following examples are for versions of PHP 4 greater than 4.0.2. See the function entries for is_uploaded_file() and move_uploaded_file().
|
The PHP script which receives the uploaded file should implement whatever logic is necessary for determining what should be done with the uploaded file. You can for example use the $HTTP_POST_FILES['userfile']['size'] variable to throw away any files that are either too small or too big. You could use the $HTTP_POST_FILES['userfile']['type'] variable to throw away any files that didn't match a certain type criteria. Whatever the logic, you should either delete the file from the temporary directory or move it elsewhere.
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.