Php uploads and downloads images

Source: Internet
Author: User
The following content of this article describes how to upload and download images through php. the following content of this article describes how to upload and download images through php.

Build a front-end page for uploading files

Here, with the help of the bootstrap front-end framework and the fontawesome icon Library, the content is very simple as an image upload box and a submit button. the code is as follows:

 

Where:
The hidden field is mainly used to upload the current file size. it is set to 2 M => 2*1024*1024 => 2097152
Set the type of the received file to the image format. you can upload multiple images. Note that when the value of name is file [], the server can receive multiple images.

Background server image Upload processing

After a file is uploaded from the front end to the server, the server processes the uploaded file information, including the following:

First, there is no error in image upload. if there is no next step, an error message is returned.

Check File upload size

Checks the file type to see if it is an image type.

Check whether the image type is true. some images may be forged. for example, if we change a text file extension to the image type

Move temporary files on the server to the specified directory

For steps 2 and 3, someone may ask, we have not limited the upload size in the hidden domain on the front end, but also set the file receiving type. why should the server perform verification? The server never believes in the data transmitted from the client. anyone who has programming experience knows that we can modify the front-end page structure and content in the browser, in addition, data can be forged. the front-end verification only serves as a filter. the server still needs to verify the data transmitted from the front-end.

Next, let's look at the code and process the file upload function upload_fun.php:

 $ Val) {$ files [$ I] ["name"] = $ 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 the file extension * @ param $ filename: file name * @ return string extension */function getExt ($ filename) {return strtolower (pathinfo ($ filename, PATHINFO_EXTENSION) );}/*** Generate a unique string as the file name * @ return string unique file name */function getUniName () {return md5 (uniqid (microtime (true), true ));} /*** main file Upload processing module * @ param $ fileInfo: file information * @ param string $ path: File Upload path * @ param bool $ flag: whether to enable verification for true images * @ 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*1024, $ allo WExt = ["jpeg", 'jpg ', 'PNG', 'GIF']) {$ res = []; if ($ fileInfo ['error'] = UPLOAD_ERR_ OK) {$ ext = getExt ($ fileInfo ["name"]); $ uniName = getUniName (); $ dest = $ path. "/". $ uniName. ". ". $ ext; // Check the size of the uploaded file if ($ fileInfo ["size"]> $ maxSize) {$ res ["msg"] = $ fileInfo ["name"]. "The uploaded file is too large";} // if (! In_array ($ ext, $ allowExt) {$ res ["msg"] = $ fileInfo ["name"]. "Illegal File type";} // check whether the image is real if ($ flag) {if (! Getimagesize ($ fileInfo ["tmp_name"]) {$ res ["mes"] = $ fileInfo ["name"]. "Not a real image" ;}}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"]. "The file is uploaded successfully"; $ res ["dest"] = $ dest;} return $ res;} else {// Determine the error message switch ($ fileInfo ["error"]) {case 1: $ res ["mes"] = "the uploaded file exceeds the value of the upload_max_filesize option in the php configuration file"; break; case 2: $ res ["mes"] = "exceeds the size limit of form MMAX_FILE_SIZE"; break; case 3: $ res ["mes"] = "file part uploaded"; break; case 4: $ res ["mes"] = "no files 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 and processes the file upload function upload_fun.php for processing:

 ';    $uploadFiles[] = $res["dest"];}$uploadFiles = array_values(array_filter($uploadFiles));print_r($uploadFiles);

$ UploadFiles = array_values (array_filter ($ uploadFiles, so we need to filter it and assign it to a new array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.