in the development of PHP program, file upload is a very common function, and it is also one of the necessary skills of PHP programmers. Happily, the ability to upload files in PHP is much simpler than in Java, C #, and other languages.
To use PHP to implement the file upload function, we first write two PHP files: index.php and upload.php. The index.php page is used to submit form requests for file uploads, and the upload.php page is used to receive uploaded files and handle them accordingly.
First of all, we have to write a simple index.php file, because it involves the main HTML code, relatively simple, so no longer repeat, the detailed code of the index.php page is as follows:
<?php//set code to UTF-8 to avoid Chinese garbled header (' content-type:text/html;charset=utf-8 '); ><! DOCTYPE html>
It is important to note that because it does not support file uploads at the beginning of the design of the HTTP protocol, the default value of the Encrypt property of the form form is application/x-www-form-urlencoded, which can only be used to submit generic form requests. If the submitted form contains a file that needs to be uploaded, we need to change the Enctype property value to Multipart/form-data to implement the file upload feature. In addition, the attribute value of method must be post.
Next, we continue to write the code for the upload.php file.
<?php//set code to UTF-8 to avoid Chinese garbled header (' Content-type:text/html;charset=utf-8 ' ); $first _file = $_files[' upload_file1 '); Get information about file 1 $second_file = $_files[' Upload_file2 ']; Get the information for file 2 $upload_dir = ' d:/upload/'; Save uploaded files directory//processing uploaded file 1if ($first _file[' error ') = = UPLOAD_ERR_OK) {//Upload file 1 temporary storage path on the server $temp _name = $first _file[' Tmp_ Name ']; Upload file 1 The real name on the client computer $file _name = $first _file[' name ']; Move the files in the Temp folder 1 to the directory where you uploaded the files, and rename them to the real name Move_uploaded_file ($temp _name, $upload _dir. $file _name); Echo ' [File 1] upload successful!<br/> ';} else{echo ' [File 1] upload failed!<br/> ';} Processing the uploaded file 2if ($second _file[' error '] = = UPLOAD_ERR_OK) {//Upload file 2 temporary storage path on the server $temp _name = $second _file[' tmp_name ']; Upload file 2 The real name on the client computer $file _name = $second _file[' name ']; Move the files in the Temp folder 2 to the directory where you uploaded the files, and rename them to the real name Move_uploaded_file ($temp _name, $upload _dir. $file _name); Echo ' [File 2] upload successful!<br/> ';} else {echo ' [File 2] upload failed!<br/> ';}?
In PHP, when a browser client submits a form request that contains an uploaded file, PHP temporarily stores the uploaded file in a temporary directory (in the Windows operating system, the default temp directory is typically c:/windows/temp). The information about the uploaded file is then stored in the hyper-global variable $_files. Therefore, we only need to get the uploaded file information through the $_files array, and then do the corresponding processing operation. Let's take a look at the details of using the Print_r () function to output a hyper-global variable $_files when uploading a.gif and b.gif two image files via a browser:
Array ([upload_file1] = = Array ( [name] + a.gif (real file name when client uploads) [Type] = Image/gif (type of file) [Tmp_name] => ; C:\Windows\Temp\php9803.tmp (the path that is temporarily stored after the file has been uploaded to the PHP server) [ERROR] = 0 (Bad message, 0 means 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] = 0 [Size] = 93111))
In the example above, the two file names we uploaded were Upload_file1 and upload_file2. Now, let's make multiple files in the form upload_file with the same parameter name, and upload the two files you just uploaded in the form of a parameter array. At this point, we need to modify the two file fields in the index.php page to the following HTML code:
In addition, we need to modify the upload.php page accordingly:
<?php//set encoding to UTF-8 to avoid Chinese garbled header (' content-type:text/html;charset=utf-8 '); $fileArray = $_files[' Upload_file '];/ /Get information about multiple files, note: The key name here does not contain [] $upload _dir = ' d:/upload/'; Save the directory where you uploaded the file, foreach ($fileArray [' Error '] as $key = + $error) { if ($error = = UPLOAD_ERR_OK) {//php constant UPLOAD_ERR_OK =0, indicates that the upload error $temp _name = $fileArray [' Tmp_name '] [$key]; $file _name = $fileArray [' name '] [$key]; Move_uploaded_file ($temp _name, $upload _dir. $file _name); echo ' upload [file '. $key. '] Success!<br/> '; } else { echo ' upload [file '. $key. '] Failed!<br/> '; }? >
Similarly, we use the Print_r () function to view the details of the Hyper 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:\Window S\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 configuration of PHP, upload the file size beyond a certain range will be error, please refer to the article at the end of how to modify the PHP upload file size limit problem resolution.
Note 2: The PHP code uploaded by the above processing file is just a simple introductory example and cannot be used directly as a formal code, as there are many security considerations that require additional attention, such as the type of file, the size of the file, and the duplicate name of the uploaded file.
Note 3: If the uploaded file name contains Chinese, it may cause the file name garbled problem. At this point, you need to use the function iconv () to convert the file name encoding.
Earlier we learned how to use PHP for file uploads and multiple file uploads. However, in the default configuration of PHP, when the uploaded file size 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 general meaning of the above error message is that we use the PO The amount of data submitted by St request exceeds the maximum server limit (8388608 bytes =8mb). The reason for the above error is that, in the PHP configuration file php.ini, the following configuration information (in php.ini, the semicolon ";" in the beginning of the line) is present by default. Indicates that the current line is a comment and will not take effect): The maximum time allowed by the script to parse the input data (like POST and GET), in seconds. It is measured from receiving all the data to starting the execution script. Max_input_time = 60; Allows the client to send a single post request the maximum data Post_max_size = 8M, whether to open the file upload function file_uploads = on; file Upload temporary storage directory (if not specified, use the system default temp directory ); upload_tmp_dir =; maximum file size allowed for single request uploads upload_max_filesize = 2M; Maximum number of files allowed to be uploaded simultaneously for a single POST request Max_file_uploads = 20
As we can see from the configuration information above, PHP's default configuration information is the "culprit" that causes the file size to exceed the limit when PHP files are uploaded. In the above configuration information, the author has given the Chinese annotation information corresponding to the various instruction options, and we can modify the php.ini configuration file according to your actual needs.