PHP implementation of two ways to upload files

Source: Internet
Author: User
Tags php example php file upload
This article is mainly for you to introduce two kinds of PHP file upload implementation method, interested friends can refer to

File uploads generally have the following 2 ways:

There are two kinds:
1, the standard input form way, typical use $_files to receive;
2, in the way of Base64 transmission, is generally Ajax asynchronous upload.

First Kind
Standard input form, suitable for large files to upload, while supporting batch. A few key words of the HTML code:

<form enctype= "Multipart/form-data" method= "post" action= "upload.php" ">  <input type=" file "Name=" Id_ Pic[] "accept=" image/* "class=" Form-control "multiple/> <input  type=" Submit "value=" Upload "/></form>

When different name:

<form enctype= "Multipart/form-data" method= "post" action= "upload.php" ">  <input type=" file "Name=" Id_ Pic_1 "accept=" image/* "class=" Form-control "/>  <input type=" file "Name=" id_pic_2 "accept=" image/* "class=" Form-control "/>  <input type=" Submit "value=" Upload "/></form>

where enctype= "Multipart/form-data" is essential for file uploads. In addition type= "file" to set the input type, accept= "image/*" specifies the first upload picture (MIME reference manual). Multiple supports selecting multiple files at once, pic[] receives multiple files as an array. Mobile phone end can also add parameters capture= "camera" select camera to upload photos.

Back-end Processing:
Get the uploaded file via $_files.

$files = $_files;
When multiple files are passed, the returned $_files array format is different if name is different.

Name at the same time:

Array (1) {["id_pic"] = = Array (5) {  ["name"] = + Array (2) {   [0] = = string (5) "1.jpg"   [1] = = String ( 5) "2.jpg"  }  ["type"] = + Array (2) {   [0] = = string (Ten) "Image/jpeg"   [1] = = string (Ten) "image/ JPEG "  }  [" tmp_name "] + = Array (2) {   [0] = = string" C:\Windows\Temp\php7A7E.tmp "   [1] = = String "C:\Windows\Temp\php7A7F.tmp"  }  ["error"] = + Array (2) {   [0] = = Int (0)   [1] = = Int (0)  }  ["size"] = + Array (2) {   [0] = = Int (77357)   [1] = Int (56720)  }}}

When name is not the same:

   Array (2) {["id_pic_1"] + = Array (5) {  ["name"] + = string (5) "1.jpg"  ["type"] + string (Ten) "Image/jpeg" c3/>["Tmp_name"] = = string "C:\Windows\Temp\phpBBEE.tmp"  ["error"] + int (0)  ["size"] + = Int ( 77357)} ["id_pic_2"] + = Array (5) {  ["name"] + = string (5) "2.jpg"  ["type"] = = string (Ten) "Image/jpeg" 
  ["Tmp_name"] = = string "C:\Windows\Temp\phpBBEF.tmp"  ["error"] + int (0)  ["size"] + = Int ( 56720)}}

In the case of a foreach traversal of the $_files, the preceding output format is not very convenient. The latter type can be traversed directly. 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; }

OK, before we talk about how the backend handles the received $_files array and converts it into a uniform format. The following tasks are mainly:
1, detection of uploaded files is illegal;
2, check whether the uploaded file is larger than the size;
3, the detection of the existence of the path to save, whether it can be written;
4, file renaming;

An important function is used in uploading: move_uploaded_file (filename, $destination) for file movement. Move $_files[' id_pic ' [' tmp_name '] to the new path. Of course, before moving can be used is_uploaded_file ($_files[' id_pic ' [' tmp_name ']) to determine whether the file is normally uploaded.

Multi-File upload is a looping method that uses Move_uploaded_file () to move operations multiple times.

The second Kind
Mainly to upload pictures mainly.
Using the Change event of input, the picture is processed with canvas (for example, compressed), and then Ajax sends the file stream to the backend.

The rationale is to render the picture through the canvas, and then compress it into a base64 string (a picture that can be compiled into a JPG format) using the Todataurl method.

Back-end Processing:
The backend eventually receives the Base64 string sent by the front end, and the string is then processed as a picture. Specifically, use the keyword base64 to image development language for Google | Baidu. There is a base64len in the result of the front-end generation, which is the length of the string, and the backend should check to see if the submission is complete.

php Example: $img = Base64_decode ($_post[' img "); $img = imagecreatefromstring ($img);

Summary: The above is the entire content of this article, I hope to be able 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.