PHP for multi-image upload and single-image upload function

Source: Internet
Author: User
This article through the example code to introduce to you based on the PHP implementation of multi-image upload and single-image upload function, the code is easy to understand, very good, with a certain reference value, the need for friends can refer to the next

The following section of code to introduce PHP implementation of single-image upload and multi-image upload function, the specific code is as follows:

/** * Multi-select picture upload * * @version v1.0.0 * @author * @since 17-11-24 */Public Function upload () {$file = $_f    iles[' file ';    Empty ($file) && $this->response (201, ' Please select the file to upload ');    unset ($_files[' file ');       $count = count ($file [' name ']);    Number of uploaded images $count > && $this->response (203, ' bulk upload images upload up to 10 images at a time ');    $tmpFile = [];    $returnData = [];      for ($i =0; $i < $count; $i + +)//Cycle picture {$tmpFile [' name '] = $file [' name '] [$i];      $tmpFile [' type '] = $file [' type '] [$i];      $tmpFile [' tmp_name '] = $file [' Tmp_name '] [$i];      $tmpFile [' error '] = $file [' ERROR '] [$i];      $tmpFile [' size '] = $file [' Size '] [$i];      $_files[' File_ '. $i] = $tmpFile; Determines whether the allowed picture type $ext = substr ($_files[' file_ '. $i [' Name '],strrpos ($_files[' file_ '. $i [' name '], '. ') +1);      Upload file suffix stripos (' jpeg|png|bmp|jpg ', $ext) = = = FALSE && $this->response (210, ' image format support JPEG, PNG, BMP format Picture '); $data = $this->uploadone (' File_ '. $i, ' jpeg|pNg|bmp|jpg '); if ($data [' status '] = = 1) {$this->response (500, ' No. '). $i + 1). '      Image upload failed, '. $data [' msg ']);   } $returnData [$i] [' url '] = $data [' url ']; The picture path $returnData [$i] [' old_name '] = substr ($tmpFile [' Name '],0,strrpos ($tmpFile [' name '], '. '));  The original name of the picture} $this->response ("successful", $returnData);   */** * Single File Upload * @version v1.0.0 * @author * @since 17-11-24 * @param $file Upload Form name * @param $type Upload type * @param $maxSize upload file limit size (default 10M) */Private Function uploadone ($filename = ' file ', $type = ' jpeg|png|bmp|jpg ', $maxSize = 10240) {list ($width, $height) = getimagesize ($_files[$filename [' tmp_name ']);//Get the picture's width and height list ($use    C, $sec) = Explode ("", Microtime ());                         $time = $sec. substr ($usec, 2); Number of seconds + microseconds $ext = substr ($_files[$filename [' Name '],strrpos ($_files[$filename] [' name '], '. ') +1); Upload file suffix $name = $time. '-'. $width. ' * '. $height. '.    $ext; $filePath = $_files[$filename [' tMp_name '];    $type = $_files[$filename [' type '];    $this->load->library (' Qiniu ');    $returnData [' url '] = $this->qiniu->upload ($name, $filePath, $type);    $returnData [' status '] = 0;  return $returnData; }

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.