Let's look at the project structure chart and the database structure chart first.
Project structure:
Operation effect;
up.html Simple Upload Form file
The code is as follows |
Copy Code |
<form action= "up.php" enctype= "Multipart/form-data" method= "POST" Name= "UploadFile" > Upload file: <input type= "file" Name= "Upfile"/><br> <input type= "Submit" value= "Upload"/></form> |
Up.php is used to process picture file uploads
The code is as follows |
Copy Code |
<?php Print_r ($_files["Upfile"]); if (Is_uploaded_file ($_files[' upfile '] [' tmp_name '])) { $upfile =$_files["Upfile"]; Gets the value inside the array $name = file name of the uploaded file $upfile ["name"];//] $type = $upfile [' type '];//the type of upload file] $size = $upfile [size]];//upload files $tmp _name= $upfile ["tmp_name"];//upload file temporary storage path /* This is a very simple way to upload files. For security reasons, you should increase the restrictions on what users have permission to upload files. Upload Restrictions In this script, we have added restrictions on file uploads. Users can only upload. gif or. jpeg files */ Switch ($type) { Case ' image/pjpeg ': $okType =true; Break Case ' image/jpeg ': $okType =true; Break Case ' image/gif ': $okType =true; Break Case ' image/png ': $okType =true; Break } if ($okType) { /** * 0: File Upload success <br/> * 1: File size exceeded, set <br/> in php.ini file * 2: exceeds the size of the file the value specified by the max_file_size option <br/> * 3: Files are only partially uploaded <br/> * 4: No files are uploaded <br/> * 5: Upload file size of 0 */ $error = $upfile ["Error"];//the value returned by the system after uploading echo "================<br/>"; echo "Upload file name is:". $name. " <br/> "; echo "Upload file type is:". $type. " <br/> "; echo "Upload file size is:". $size. " <br/> "; echo "The value returned by the system after uploading is:". $error. " <br/> "; echo "The temporary storage path for the uploaded file is:". $tmp _name. " <br/> ";
echo "Start moving upload file <br/>"; Move the uploaded temporary files to the up directory below Move_uploaded_file ($tmp _name, ' up/'. $name); $destination = "up/". $name; echo "================<br/>"; echo "Upload information:<br/>"; if ($error ==0) { echo "File uploaded successfully!" "; echo "<br> picture preview:<br>"; echo " "; echo "alt=" Picture preview: R filename: ". $destination." R upload time: ">"; }elseif ($error ==1) { echo "Exceeds file size, set in php.ini file"; }elseif ($error ==2) { echo "exceeds the size of the file max_file_size the value specified by the option"; }elseif ($error ==3) { echo "file is only partially uploaded"; }elseif ($error ==4) { echo "No file was uploaded"; }else{ echo "Upload file size is 0"; } }else{ echo "Please upload a picture in jpg,gif,png format!" "; } } ?> |
Summary and Analysis
In the PHP image upload we mainly through the use of PHP's global array $_files, you can upload files from the client computer to the remote server.
The first parameter is the input name of the form, and the second subscript can be "name", "type", "Size", "tmp_name", or "error". Just like this:
$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["Type"]-type of file being uploaded
$_files["File" ["Size"]-the size of the file being uploaded, in bytes
$_files["File" ["Tmp_name"]-name of the temporary copy of the file stored on the server
$_files["File" ["Error"]-error code caused by file upload
As long as you understand the above parameters can be fully implemented in PHP file upload Oh, because I limit the above
code is as follows |
copy code |
switch ($type) { Case ' image/pjpeg ': $okType =true; break; case ' image/jpeg ': $okType =true; break; case ' image/gif ': $okType =true; break; case ' image/png ': $okType =true; break; } |
The
can only upload jpg,gif,png,jpeg four format picture files, so this is the picture upload implementation.