PHP implements file upload and multi-file Upload

Source: Internet
Author: User
Tags php file upload
This article mainly introduces PHP file upload and multi-file Upload related information, and also shares with you how to modify the size limit of PHP file upload, if you need it, you can refer to the PHP program development. file upload is a very common function and is also one of the essential skills of PHP programmers. It is worth noting that implementing the file upload function in PHP is much simpler than in Java, C #, and other languages. Next we will introduce in detail how to use PHP to implement file upload and multifile Upload functions based on specific code examples.

To use PHP for file upload, we first compile two php files: index. php and upload. PHP. The index. php page is used to submit a form request for File upload. the upload. php page is used to receive and process uploaded files.

First, we will compile a simple index. php file. because it involves mainly html code and is relatively simple, we will not go into details. the detailed code of the index. php page is as follows:

<? Php // Set the encoding to UTF-8 to avoid Chinese garbled header ('content-Type: text/html; charset = utf-8 ');?>  File upload form page

It is worth noting that, at the beginning of the HTTP protocol design, it does not support the file upload function. the default value of the encrypt attribute of the form is application/x-www-form-urlencoded, it can only be used to submit common form requests. If the submitted form contains the file to be uploaded, we need to change the value of enctype to multipart/form-data to implement the file upload function. In addition, the property value of method must be post.

Next, we will continue to compile the upload. php file code.

<? Php // Set the encoding to UTF-8 to avoid Chinese garbled headers ('content-Type: text/html; charset = utf-8 '); $ first_file = $ _ FILES ['upload _ file1']; // Obtain file 1 information $ second_file = $ _ FILES ['upload _ file2']; // Get File 2 information $ upload_dir = 'd:/upload /'; // Save the Directory of the uploaded file // process the uploaded file 1if ($ first_file ['error'] = UPLOAD_ERR_ OK) {// temporary storage path of file 1 on the server $ temp_name = $ first_file ['tmp _ name']; // upload file 1's real name on the client computer $ file_name = $ first_file ['name']; // Move file 1 in the temporary folder to the directory where the uploaded files are stored and Rename it to the real name move_uploaded_file ($ temp_name, $ upload_dir. $ file_name); echo '[file 1] uploaded successfully!
';} Else {echo' [file 1] Upload failed!
';} // Process the uploaded file 2if ($ second_file ['error'] = UPLOAD_ERR_ OK) {// temporary storage path of file 2 uploaded on the server $ temp_name = $ second_file ['tmp _ name']; // the real name of file 2 uploaded on the client computer $ file_name = $ second_file ['name']; // Move File 2 in the temporary folder to the directory where the uploaded files are stored, rename move_uploaded_file ($ temp_name, $ upload_dir. $ file_name); echo '[File 2] uploaded successfully!
';} Else {echo' [File 2] Upload failed!
';}?>

In PHP, when the form request submitted by the browser client contains the uploaded file, PHP will temporarily store the uploaded file in the temporary directory (in Windows operating system, the default temporary directory is generally C:/Windows/Temp, and the information about the uploaded file is stored in the Super global variable $ _ FILES. Therefore, we only need to get the information of the uploaded file through the $ _ FILES array, and then perform corresponding processing operations on it. Next, when we see the.gif and B .gif image FILES uploaded on the browser, use the print_r () function to output the details of the Super global variable $ _ FILES:

Array ([upload_file1] => Array ([name] => A.gif (real file name during client Upload) [type] => image/gif (file type) [tmp_name] => C: \ Windows \ Temp \ php9803.tmp (path for temporary storage after the file is uploaded to the PHP Server) [error] => 0 (error message, 0 indicates no error) [size] => 87123 (file size, in bytes )) [upload_file2] => Array ([name] => B .gif [type] => image/gif [tmp_name] => C: \ Windows \ Temp \ php9813.tmp [error] = & gt; 0 [size] = & gt; 93111 ))

In the preceding example, the parameter names of the uploaded files are upload_file1 and upload_file2. Now, we have multiple files in the form with the same parameter name upload_file, and submit the two files that have just been uploaded again in the form of a parameter array. In this case, we need to modify the two file fields on the index. php page to the following html code:

  • File 1:
  • File 2:

In addition, we need to modify the upload. php page accordingly:

<? Php // Set the encoding to UTF-8 to avoid Chinese garbled headers ('content-Type: text/html; charset = utf-8 '); $ fileArray = $ _ FILES ['upload _ file']; // obtain information about multiple FILES. note: The key name here does not include [] $ upload_dir = 'd: /upload/'; // Save the foreach Directory of the uploaded file ($ fileArray ['error'] as $ key => $ error) {if ($ error = UPLOAD_ERR_ OK) {// PHP constant UPLOAD_ERR_ OK = 0, indicating no Upload error $ temp_name = $ fileArray ['tmp _ name'] [$ key]; $ file_name = $ fileArray ['name'] [$ key]; move_uploaded_file ($ temp_name, $ u Pload_dir. $ file_name); echo 'upload [file '. $ key.'] successful!
';} Else {echo' an error occurred while uploading the [file '. $ key!
';}}?>

Similarly, we use the print_r () function to view the details of the Super global variable $ _ FILES in the above example:

Array (  [upload_file] => Array (  [name] => Array (   [0] => A.gif  [1] => B.gif    )  [type] => Array (   [0] => image/gif  [1] => image/gif    )  [tmp_name] => Array (  [0] => C:\Windows\Temp\php87B9.tmp  [1] => C:\Windows\Temp\php87BA.tmp  )  [error] => Array (   [0] => 0  [1] => 0    )  [size] => Array (   [0] => 87123  [1] => 93111    ) ))

Note 1: under the default PHP configuration, an error will occur when the size of the uploaded file exceeds a certain range. please refer to the solution to the problem of modifying the size limit of the PHP uploaded file mentioned at the end of the article.
Note 2: The above PHP code for processing file uploads is just a simple Getting started example and cannot be used directly as the official code, because there are still many security factors that require additional attention, for example: the file type, file size, and name of the uploaded file are repeated.
Note 3: If the name of the uploaded file contains Chinese characters, the file name may be garbled. In this case, you need to use the iconv () function to convert the file name encoding.

We learned how to use PHP to upload files and multiple files. However, in the default PHP configuration, when the size of the uploaded file exceeds a certain limit, we will get the following error message:

Warning: POST Content-Length of 625523488 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 the above error message roughly means, the size of data submitted using the POST request exceeds the server's maximum limit (8388608 bytes = 8 MB ). The cause of the above error is that the configuration file PHP. in ini, the following configuration information (in php. in ini, the semicolon ";" at the beginning of the line indicates that the current line is a comment and will not take effect):; the maximum time allowed by the script to parse input data (similar to POST and GET), in seconds. It starts from receiving all the data to executing the script for measurement. Max_input_time = 60; maximum data post_max_size = 8 M allowed for a single POST request from the client; whether to enable the file upload function file_uploads = On; temporary directory for file upload (if not specified, use the default temporary directory of the system); upload_tmp_dir =; maximum file size that can be uploaded by a single request upload_max_filesize = 2 M; maximum file size that can be uploaded by a single POST request simultaneously max_file_uploads = 20

From the above configuration information, we can see that the default configuration information of PHP is the "culprit" that causes the file size to exceed the limit when uploading PHP files 」. I have provided the Chinese comments corresponding to each command option in the above configuration information. you can modify the php. ini configuration file according to your actual needs.

The above is all the content of this article, helping you implement the php file upload function.

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.