How to use $_files in PHP and notes on precautions

Source: Internet
Author: User
Tags html form http post

$_files: Variable submitted to script via HTTP POST file upload, similar to old array $http_post_files array (still valid but opposed to use) details can be found in POST method upload

The contents of the $_files array are as follows:

$_files[' myFile ' [' Name '] The original name of the client file

The MIME type of the $_files[' myFile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif"

$_files[' myFile ' [' size '] the size of the uploaded file, in bytes

$_files[' myFile ' [' tmp_name '] files are uploaded on the server after the temporary file name stored in the system is generally the default, can be specified in PHP.ini Upload_tmp_dir, but with the putenv () function setting is not working

$_files[' myFile ' [' Error '] and the file upload related error code

UPLOAD_ERR_OK value: 0; No error occurred, file upload succeeded

Upload_err_ini_size value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.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; Files are only partially uploaded

upload_err_no_file Value: 4; No file is uploaded, value: 5; Upload file size is 0


1. after the file is uploaded, it is stored in the temp directory by default, and it must be removed from the temp directory or moved elsewhere, if not, it will be deleted. That is, regardless of whether or not the upload succeeds, the files in the temp directory will be deleted after the script executes. So before you delete it, copy it to another location using PHP's copy () function, and it's time to upload the file.

2. when uploading a file with a form, be sure to add the attribute content enctype= "Multipart/form-data", otherwise you will be reported an exception when obtaining the file information with $_files[filename].

<formenctype= "Multipart/form-data"Action= "URL"Method= "POST">       <inputname= "MyFile"type= "File">       <inputtype= "Submit"value= "Upload file"></form>

3. Move method Move_uploaded_file (string $filename , string $destination ), the filename specified file is a valid upload file (that is, the HTTP POST via PHP Uploaded by the upload mechanism). If the file is valid, move it to the destination specified file. If the filename does not have an action, the function returns false and overwrites if the target file already exists.

How to use $_files in PHP and notes on precautions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.