: This article describes how to upload multiple PHP files. For more information about PHP tutorials, see. Before talking nonsense, we should first declare that this article is based on the understanding of php single-file upload, so we will not go into details about the file upload server configuration here, and pay attention to the form settings.
There are not many theme statements. There are two writing methods in the request page (only the form part is displayed. the preceding three files are uploaded as an example .)
Select my uploaded files
Select my uploaded files
The two comparisons show that only the name is different. The first one sets the name as an array, and the second one is a method that we usually set easily.
Although on the surface, the $ _ FILES actually submitted to the doAction. php page are quite different.
The first $ _ FILES is a three-dimensional array, and the second is a two-dimensional array, as follows:
Obviously, it is more convenient to process $ _ FILES in the second format. Of course, we can also try to convert the $ _ FILES in the first format into the second format, as follows :\
function getFiles(){ foreach($_FILESas$file){ $fileNum=count($file['name']); if ($fileNum==1) { $files=$file; }else{ for ($i=0; $i < $fileNum; $i++) { $files[$i]['name']=$file['name'][$i]; $files[$i]['type']=$file['type'][$i]; $files[$i]['tmp_name']=$file['tmp_name'][$i]; $files[$i]['error']=$file['error'][$i]; $files[$i]['size']=$file['size'][$i]; } } } return$files;}
Use this function to convert $ _ FILES to the following format:
At this moment, the two Upload methods are at the same starting point. The following work is to compile the uploadFile () function to upload each file, which is also the focus of this article.
File upload function:
Function uploadFile ($ file, $ path = '. /uploads ', $ max_size, $ allowExt) {$ filename = $ file ['name']; $ type = $ file ['type']; $ temp_name = $ file ['tmp _ name']; $ error = $ file ['error']; $ size = $ file ['size']; if ($ error = UPLOAD_ERR_ OK) {if ($ size> $ max_size) {$ res ['Mes '] = $ filename. "The file size exceeds the specified Upload size";} $ ext = getExt ($ filename); if (! In_array ($ ext, $ allowExt) {$ res ['Mes '] = $ filename.' the file name does not conform to the specifications ';} if (! Is_uploaded_file ($ temp_name) {$ res ['Mes '] = $ filename. "The file is not uploaded through the http post method";} if (@ $ res) {return $ res;} if (! File_exists ($ path) {mkdir ($ path, 0777, true); chmod ($ path, 0777) ;}$ fname = getUniName (); $ destination = $ path. '/'. $ fname. '. '. $ ext; if (move_uploaded_file ($ temp_name, $ destination) {$ res ['Mes '] = $ filename. 'uploaded successfully'; $ res ['dest'] = $ destination;} else {$ res ['Mes '] = $ filename. "File upload failed" ;}}else {switch ($ error) {case '1': $ res ['Mes '] = "exceeds the size of the configuration file to be uploaded "; break; case '2': $ res ['Mes '] = "exceeds the size of the file to be uploaded in the form setting"; break; case '3 ': $ res ['Mes '] = "partially uploaded files"; break; case '4': $ res ['Mes'] = "no files uploaded"; break; case '6': $ res ['Mes '] = "no temporary directory found"; break; case '7 ': $ res ['Mes '] = "file cannot be written"; break; default: $ res ['Mes'] = "file Upload failed"; break ;}} return $ res ;}
Two small functions are involved:
/*** Get the file extension * @ param string $ filename Upload file name * @ return string returns the extension */function getExt ($ filename) {$ arr = explode ('. ', basename ($ filename); returnend ($ arr );} /*** get the unique file extension * @ return string after md5, generate a 32-bit unique Upload file name */function getUniName () {returnmd5 (uniqid (microtime (true ), true ));}
Feelings:
I had access to PHP file upload a long time ago, and I felt confused. Now it seems that as long as you master the information contained in $ _ FILES, you can use the knowledge you have written to process some tips. The system has logical considerations and encapsulates functions in a timely manner, in the future, files can be uploaded quickly. The code I presented this time cannot meet any needs, so you can transform it into your own code. For example, if you want to upload a file as an image content, the extension alone is absolutely unknown. you also need to use the image features for verification.
The above section describes how to upload multiple PHP files, including some content, and hopes to help anyone interested in PHP tutorials.