The attention point of files in PHP

Source: Internet
Author: User
Tags html form

HTML form for file upload:

< form enctype= "Multipart/form-data" action= "" method= "POST" > < input type= "hidden" name= "max_file_size" V alue= "1000" > < input name= "myFile" type= "file" > < input type= "submit" value= "submit" > </form &G T

Note: The enctype= "Multipart/form-data" must be written, otherwise the $_files array is empty and the value is not available.

The $_files array reads as follows:
$_files[the original name of the ' MyFile ' [' name '] 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 stored in the server after the temporary file name, typically the system default. Can be specified in the php.ini upload_tmp_dir, but the putenv () function setting does not work.
$_files[' myFile ' [' Error '] and the file upload related error code.

[' ERROR '] is added in PHP version 4.2.0. Here's what it says: (They're constant after PHP3.0)
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 upload file exceeds the value specified by the Max_file_size option in the HTML form.
Upload_err_partial
Value: 3; The file is only partially uploaded.
Upload_err_no_file
Value: 4; No files were uploaded.
Value: 5; The upload file size is 0.
When the file is uploaded, it is stored in a temporary directory by default, and you must remove it from the temporary directory or move it to another location, if not, it will be deleted. That is, whether or not the upload succeeds, the files in the temp directory will be deleted after the script is executed. So before you delete it, copy it to another location using PHP's copy () function, which completes the upload file process. Here to note:

The settings for the Enctype property in form forms in PHP.

The Enctype property in form forms specifies the type of encoding the browser uses when sending data back to the server.
Here is the value Note:
Multipart/form-data: Form data is encoded as a message, each control on the page corresponds to one part of the message, not the character encoding. This value is required when you use a form with a file upload control ...
application/x-www-form-urlencoded: Form data is encoded as a name/value pair. This is the standard encoding format. Encodes all characters (default) before sending.
Text/plain: Form data is encoded in plain text without any control or formatting characters, used in mail processing, and rarely used elsewhere. Converts a space to a "+" symbol, but does not encode special characters. Grasping the package visible data form.

Description

1 if there are files to upload in the form, form tags must be set enctype= "Multipart/form-data" to ensure the MIME encoding of the anonymous uploaded files. By default, the form's encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;

2 when the session processing, if the form set enctype= "Text/plain", then through the $_post,$_get,$_request can not be taken to the value. This must be careful ...

In a simple form upload test, the server-side PHP script, the $_files array is empty, so you can not get the information uploaded from the browser. What's the reason?
Through Google, find the following PHP upload file $_files array null solution:

Use PHP to upload file application, in the background when printing $_files found empty array, this problem may have the following two reasons:  
form type reason or PHP Setup problem: Form type  
the form encoding type of the upload file must be set to Enctype= "Multipart/form-data", because to pass large data, the general submission method with POST PHP setup problem:  
PHP's default post_max_size is 2M. If POST data size is greater than Post_max _size, $_post  and  $_FILES  Hyper global variables are empty. The solution is:  
General file uploads, unless the file is very small. Like a 5M file, it's likely to take more than a minute to upload. But in PHP, the default page execution time is 30 seconds. That means more than 30 seconds, and the script stops executing. This leads to the appearance Unable to open the page. We can then modify the Max_execution_time in php.ini to find max_execution_time default is 30 seconds. To Max_execution_time = 0 (modifiable range is php_ini_ All) 0 indicates that   is not restricted,
or Ini_set (' Max_execution_time ', 0) is set in the php file header, and the maximum size allowed for post data is modified post_max_size. This setting also affects file uploads. Find post_max_size. Change to Post_max_size = 150M (modifiable range is php_ini_php_ini_perdir) Many people will change the second step. But when uploading files, the maximum remains 8M. Why not? We also need to change a parameter upload_max_filesize represents the maximum size of the uploaded file. Find Upload_max_filesize, the default is 8M to Upload_max_filesize = 100M (modifiable range is php_ini_php_ini_perdir)  
also to be described is Post_max_ Size is the entire expression, and upload_max_filesize is the size of the uploaded file, which should be greater than the latter.

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: info-contact@alibabacloud.com 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.