The usage and notes of $_FILES in PHP

  • 2021-01-03 20:51:33
  • OfStack

$_FILES: A variable submitted to the script via an HTTP POST file upload, similar to the old array $HTTP_POST_FILES array (still valid, but opposed to use). See POST method upload for details

The $_FILES array has the following contents:

$_FILES['myFile']['name'] the original name of the client file

The MIME type of $_FILES['myFile']['type'] file that requires the browser to support this information, e.g. "image/gif"

$_FILES['myFile']['size'] size of uploaded file in bytes

$_FILES['myFile']['tmp_name'] the temporary file name stored on the server after the file has been uploaded, 1 is generally the system default. It can be specified in php.ini's upload_tmp_dir, but it does not work with the putenv() function

$_FILES['myFile']['error'] and the error code associated with this file upload, ['error'] was added in PHP 4.2.0, below is its description :(they are constant after PHP3.0)

UPLOAD_ERR_OK values: 0; No error, file upload successful

UPLOAD_ERR_INI_SIZE value: 1; The uploaded file exceeds the value restricted by the upload_max_filesize option in ES60en.ini

UPLOAD_ERR_FORM_SIZE value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form

UPLOAD_ERR_PARTIAL value: 3; Only part of the file is uploaded

UPLOAD_ERR_NO_FILE value: 4; No files uploaded, value: 5; The upload file size is 0

Note:

1. When the file is uploaded, it is stored in the temporary directory by default. At this time, it must be deleted from the temporary directory or moved to another place, if not, it will be deleted. This means that files in the temporary directory will be deleted after the script executes, regardless of whether the upload is successful or not. So before deleting it, copy it to another location using the copy() function of PHP. At this point, the uploading process is complete.

2. Before PHP 4.1.0, the array was named $HTTP_POST_FILES, and it is not an automatic global variable like $_FILES 1. PHP 3 does not support the $HTTP_POST_FILES array.

3. When using form to upload a file, 1 must add the attribute enctype="multipart/ ES108en-ES109en ", otherwise an exception will be reported when using $_FILES[filename] to obtain file information.


<form enctype="multipart/form-data" action="URL"method="post">
       <input name="myFile" type="file">
       <input type="submit"value=" Upload a file ">
</form>


Related articles: