Two methods for uploading php files

Source: Internet
Author: User
Tags php example
This article mainly introduces two methods for uploading php files. if you are interested, you can refer to the following two methods for uploading files:

There are two types:
1. standard input form, typically received with $ _ FILES;
2. it is transmitted in Base64 mode, generally AJAX asynchronous Upload.

First
The standard input form is suitable for uploading large files and batch uploading is supported. Key html code sentences:

 

For different names:

 

Enctype = "multipart/form-data" is essential for file upload. In addition, if type = "file" is used to set the input type, accept = "image/*" specifies that the image is uploaded preferentially (MIME reference manual ). Multiple supports selecting multiple files at a time. pic [] receives multiple files in the form of arrays. You can also add the parameter capture = "camera" to the mobile phone end to select a camera for upload.

Backend processing:
Get the uploaded file through $ _ FILES.

$ Files = $ _ FILES;
When multiple FILES are uploaded, if the name is different, the returned $ _ FILES array format is different.

Same name:

array(1) { ["id_pic"] => array(5) {  ["name"] => array(2) {   [0] => string(5) "1.jpg"   [1] => string(5) "2.jpg"  }  ["type"] => array(2) {   [0] => string(10) "image/jpeg"   [1] => string(10) "image/jpeg"  }  ["tmp_name"] => array(2) {   [0] => string(27) "C:\Windows\Temp\php7A7E.tmp"   [1] => string(27) "C:\Windows\Temp\php7A7F.tmp"  }  ["error"] => array(2) {   [0] => int(0)   [1] => int(0)  }  ["size"] => array(2) {   [0] => int(77357)   [1] => int(56720)  } }}


Different names:

   array(2) { ["id_pic_1"] => array(5) {  ["name"] => string(5) "1.jpg"  ["type"] => string(10) "image/jpeg"  ["tmp_name"] => string(27) "C:\Windows\Temp\phpBBEE.tmp"  ["error"] => int(0)  ["size"] => int(77357) } ["id_pic_2"] => array(5) {  ["name"] => string(5) "2.jpg"  ["type"] => string(10) "image/jpeg"  ["tmp_name"] => string(27) "C:\Windows\Temp\phpBBEF.tmp"  ["error"] => int(0)  ["size"] => int(56720) }}

The previous output format is not convenient when you perform foreach for $ _ FILES. The later one can be directly traversed. We can write a method for unified conversion:

function dealFiles($files) {    $fileArray = array();    $n     = 0;    foreach ($files as $key=>$file){      if(is_array($file['name'])) {        $keys    =  array_keys($file);        $count   =  count($file['name']);        for ($i=0; $i<$count; $i++) {          $fileArray[$n]['key'] = $key;          foreach ($keys as $_key){            $fileArray[$n][$_key] = $file[$_key][$i];          }          $n++;        }      }else{        $fileArray = $files;        break;      }    }    return $fileArray; }

Well, we will talk about how the backend processes the received $ _ FILES array and converts it to a unified format. The subsequent tasks are as follows:
1. check whether the uploaded file is illegal;
2. check whether the size of the uploaded file has exceeded;
3. check whether the saved path exists and can be written;
4. rename the file;

A very important function is used in the upload process: move_uploaded_file (filename, $ destination) to move the file. Move $ _ FILES ['id _ pic '] ['tmp _ name'] to a new path. Before moving the file, you can use is_uploaded_file ($ _ FILES ['id _ pic '] ['tmp _ name']) to determine whether the file is uploaded normally.

Multifile upload is a cyclical method that uses move_uploaded_file () for multiple moves.

Second
These images are mainly used for uploading images.
Use the change event of input to process images (such as compression) using canvas, and then send files to the backend using ajax.

The basic principle is to render the image through canvas, and then compress and save it as a base64 string (the image can be compiled into jpg format) using the toDataURL method ).

Backend processing:
The backend will eventually receive the base64 string sent by the front end, and then process the string as an image. For details, use the keyword base64 to convert the image development language to Google | Baidu. The result generated by the front end contains a base64Len, which is the length of the string. the backend should check whether the request is submitted completely.

// Php example: $ img = base64_decode ($ _ POST ['IMG ']); $ img = imagecreatefromstring ($ img );

The above is all the content of this article, hoping to help you learn.

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.