PHP combines array functions to implement multifile Upload

Source: Internet
Author: User
In PHP program development, file upload is a very common function and a required skill for 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. In PHP program development, file upload is a very common function and a required skill for 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:

     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.

 ';} 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:

 $ Error) {if ($ error = UPLOAD_ERR_ OK) {// PHP constant UPLOAD_ERR_ OK = 0, indicates that an error occurred while uploading $ temp_name = $ fileArray ['tmp _ name'] [$ key]; $ file_name = $ fileArray ['name'] [$ key]; move_uploaded_file ($ temp_name, $ upload_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: In the default PHP configuration, an error occurs when the size of the uploaded file exceeds a certain range.

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 function iconv () to convert the file name encoding.


[Recommended tutorials]

1. related topic recommendations: php Array (Array)


The above is PHP's comprehensive use of array functions-to achieve multi-file upload details, please pay attention to other articles in the first PHP community!

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.