This article mainly introduces the PHP implementation upload multi-file sample code related data, with a certain reference value, interested in small partners can refer to.
Uploading Multiple files
1. Before the operation of the multi-file information, it is necessary to construct the file information and convert the three-dimensional array into a two-dimensional array for file operation through the Buildinfo function.
function Buildinfo () { $i = 0; foreach ($_files as $v) { //determines whether the single -file if (is_string ($v [' name '])) { $files [$i] = $v; $i + +; } else{ //multi-file foreach ($v [' name '] as $key + = $value) { $files [$i] [' name '] = $value; $files [$i] [' size '] = $v [' Size '] [$key]; $files [$i] [' tmp_name '] = $v [' Tmp_name '] [$key]; $files [$i] [' type '] = $v [' type '] [$key]; $files [$i] [' error '] = $v [' ERROR '] [$key]; $i + + ; }}} return $files;}
Single-File and multi-file is determined by the type of file array, single-file (two-dimensional array), multi-file (three-dimensional array).
2. File upload function and parameters
Copy the Code code as follows:
function Uploadfiles ($path = "uploads", $allowExt = Array ("JPG", "gif", "PNG", "wbmp"), $maxSize = 1048576, $imgFlag = Tru E
Path, save the directory. Allowext, allowing extended an array groups. MaxSize maximum upload file size. Imgflag, image file identifier.
3. Determine if the directory where the file is saved exists
if (!file_exists ($path)) { mkdir ($path, 0777, True); }
4. Pass in the built-in file information and save it to files
$i = 0; $files = Buildinfo ();
5. Determine the error type of the file
foreach ($files as $file) { if ($file [' error '] = = UPLOAD_ERR_OK) {}else{ switch ($file [' ERROR ']) {case 1: $mes = "Upload file size over profile"; break; Case 2: $mes = "The form setup was exceeded"; break; Case 3: $mes = "File part is uploaded"; break; Case 4: $mes = "no file upload"; break; Case 6: $mes = "No temporary directory found"; break; Case 7: $mes = "File is not writable"; break; Case 8: $mes = "File upload interrupted due to PHP extension"; break; }
6. Restricting file types
if ($file [' error '] = = UPLOAD_ERR_OK) { $ext = getext ($file [' name ']); if (!in_array ($ext, $allowExt)) { exit ("Illegal file type"); }
7. Limit whether the image type is true
if ($imgFlag) { if (!getimagesize ($file [' tmp_name '])) { exit ("Not true picture type");} }
8. Determine file size
if ($file [' Size '] > $maxSize) { exit ("File too Large"); }
9. Determine if the file is uploaded via post
if (!is_uploaded_file ($file [' tmp_name '])) {exit ("file is not uploaded via HTTP POST");}
10. Rename the file (unique and not duplicated) and save to the destination directory
$filename = Getuniname (). ".". $ext; $destination = $path. " /". $filename, if (Move_uploaded_file ($file [' Tmp_name '], $destination) { $mes =" File upload succeeded "; $file [' name '] = $filename; unset ($file [' Error '], $file [' tmp_name ']); $uploadedFiles [$i] = $file; $i + +; }
Rewrite the name of the file information array, delete the variable file[' error ' and file[' Tmp_name ']. Finally, the successfully uploaded file is saved to the Uploadedfiles array.
The above is the whole content of this article, I hope that everyone's study has helped.