The next part of this article is about how to upload and download images via PHP.
Building the upload file front-end page
Here I use the bootstrap front-end frame and the Fontawesome icon Library, the content is simply a picture upload box and a Submit button, the code is as follows:
<!--form.php--><div class= "container" > <form action= "upload_del.php" method= "POST" enctype= " Multipart/form-data "class=" Form-horizontal "> <input type=" hidden "name=" max_file_size "value=" 2097152 " > <div class= "btn btn-success filebox" > <span> <i class= "fa fa-file-image-o" ></ i> upload images </span> <input type= "file" accept= "image/*" Name= "file[]" multiple> </ div> <input type= "Submit" value= "Upload" class= "btn btn-primary" > </form></div>
which
<input type= "hidden" name= "max_file_size" value= "2097152" > hidden fields are mainly used for file upload size now, set to 2m=>2*1024*1024=> 2097152
<input type= "File" accept= "image/*" name= "file[" multiple> set receive file type as Picture format, can upload multiple images, note that the server can receive multiple pictures when the Name property value is file[]
Background server picture upload processing
After the file is uploaded to the server from the front end, the server gets the information to upload the file after processing, mainly including the following points:
First image upload there is no error, if there is no next step, there is a return error message
Detecting the size of file uploads
Detect file type to see if it is a picture type
Detect whether the real picture type, some pictures may be forged, such as we have a text file extension to the image type, the above are still through
Move the server temp file to the specified directory
for 2 or 3 steps one might ask, we are not the front end already limit the upload size in the hidden domain, and also set the file's receive type, why the server also to verify it? There is a saying that the server never trust the data sent by the client, have experienced programming experience, we can modify the front page structure and content in the browser, but also can forge the data, the front end of the verification is only the role of filtering, and can not once and for all, The server still has to verify the data sent by the foreground.
Next look directly at the code, processing the file upload function upload_fun.php:
<?php/** * Get upload file information, process single file and multi file upload * @return array upload file information */function getFiles () {$i = 0; $files =[]; foreach ($_files as $file) {if (Is_string ($file ["name"]) {$files [$i] = $file; $i + +; } elseif (Is_array ($file ["name"]) {foreach ($file ["name"] as $key = + $val) {$files [$i] [NA Me "] = $file [" name "] [$key]; $files [$i] ["type"] = $file ["type"] [$key]; $files [$i] [tmp_name] = $file ["Tmp_name"] [$key]; $files [$i] [ERROR] = $file ["Error"] [$key]; $files [$i] ["size"] = $file ["Size"] [$key]; $i + +; }}} return $files;} /** * Get file extension * @param $filename: File name * @return string extension */function getext ($filename) {return Strtolower (PathInfo ($fi Lename, Pathinfo_extension));} /** * Generates a unique string as file name * @return string unique file name */function Getuniname () {return MD5 (Uniqid (Microtime (True), true));} /** * Upload file main processing module * @param $fileInfo: File information * @paramString $path: Upload file path * @param bool $flag: Whether to turn on validation as true picture * @param int $maxSize: Maximum file Upload size * @param array $allowExt: Allowed file extension * @ Return array information */function upload_file ($fileInfo, $path = "./uploads", $flag = true, $maxSize = 2*1024 * 1024x768, $ALLOWEXT = ["JPEG", ' jpg ', ' png ', ' gif ')] {$res = []; if ($fileInfo [' error '] = = UPLOAD_ERR_OK) {$ext = Getext ($fileInfo ["name"]); $uniName = Getuniname (); $dest = $path. "/" . $uniName. "." . $ext; Detects the upload file size if ($fileInfo ["size"] > $maxSize) {$res ["msg"] = $fileInfo ["Name"]. "Upload file too large"; }//Upload file type if (!in_array ($ext, $allowExt)) {$res ["msg"] = $fileInfo ["Name"]. "Illegal file type"; }//Detect if True picture if ($flag) {if (!getimagesize ($fileInfo ["Tmp_name"]) {$res ["mes"] = $fileInfo ["Name"]. "Not the real picture"; }} if ($res) return $res; if (!file_exists ($path)) {mkdir ($path, 0777, true); } if (! @move_uploaded_file ($fileInfo ["Tmp_name"], $dest) {$res [' msg '] = $fileInfo ["Name"]. "File upload failed"; } else {$res ["msg"] = $fileInfo ["Name"]. "File upload succeeded"; $res ["dest"] = $dest; } return $res; } else {//error message switch ($fileInfo ["error"]) {Case 1: $res ["mes"] = "Upload file exceeds PHP configuration The value of the Upload_max_filesize option in the file "; Break Case 2: $res ["mes"] = "exceeds the size of the form mmax_file_size limit"; Break Case 3: $res ["mes"] = "file part is uploaded"; Break Case 4: $res ["mes"] = "No upload file selected"; Break Case 6: $res ["mes"] = "No temporary directory found"; Break Case 7: $res [' msg '] = "file Write Failed"; Break Case 8: $res ["mes"] = "system error"; Break } return $res; }}
The server receives the upload file call processing file Upload function upload_fun.php to process:
<?php//upload_del.phprequire_once "upload_fun.php"; $files = GetFiles (); foreach ($files as $fileInfo) { $res = Upload_file ($fileInfo); echo $res ["MSG"]. ' <br> '; $uploadFiles [] = $res ["Dest"];} $uploadFiles = Array_values (Array_filter ($uploadFiles));p Rint_r ($uploadFiles);
$uploadFiles = Array_values (Array_filter ($uploadFiles)), the main reason is that when uploading multiple files, there may be individual file errors and upload failure results in $uploadfiles[] A value is a null value, So we need to filter it and assign a value to a new array.