PHP File Upload Classification

Source: Internet
Author: User
<?php/*** file Upload class * @author lijiamin* @time 2017-02-17* @email 1195989301@qq.com*/class upload{private $allowExt = Array ( ' gif ', ' jpg ', ' jpeg ', ' BMP ', ' png ', ' SWF ');//limit file upload suffix private $maxSize = 1;//limit maximum file upload 1m/*** get file information * @param str $flag The identity of the uploaded file * @return An array of */public function GetInfo ($flag) {return $_files[$flag] for the upload file of arr;} /*** gets the suffix of the file * @param str $filename file name * @return str file extension */public function Getext ($filename) {return pathinfo ($filename, PA thinfo_extension);} /*** detect the upload file is legitimate * @param str $filename filename * @return bool file extension is legal */private function Checkext ($filename) {$ext = $this->g ETEXT ($filename); return In_array ($ext, $this->allowext);} /*** detect if file size exceeds limit * @param int size File size * @return bool file size exceeds limit */public function Checksize ($size) {return $size < $this ->maxsize * 1024 * 1024;} /*** random filename * @param int $len The length of the random file name * @return str random string */public function Randname ($len =6) {return substr (Str_shuffle (' ABC defghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789 '), 0, $len);} /*** create the path to the file uploaded * @retuRN str File Upload path */Public Function Createdir () {//upload file path $dir = './upload/'. Date (' y/m/d ', Time ());//Determine if the folder exists, does not exist the new if (Is_ Dir ($dir) | | mkdir ($dir, 0777,true)) {return $dir;}} /*** File Upload * @param str $flag File Upload identifier * @return Array returns the upload file name, save path */public function UploadFile ($flag) {if ($_files[$flag] [' Name '] = = = ' | | $_files[$flag] [' ERROR ']!== 0) {echo "did not upload the file"; return;} $info = $this->getinfo ($flag), if (! $this->checkext ($info [' name '])) {echo "Unsupported file type"; return;} if (! $this->checksize ($info [' size ']) {echo "File size exceeds limit"; return;} $filename = $this->randname (). '. '. $this->getext ($info [' name ']), $dir = $this->createdir (), if (!move_uploaded_file ($info [' tmp_name '), $dir. '/'. $ FileName) {echo "File upload failed";} Else{return Array (' filename ' = = $filename, ' dir ' = = $dir);}}? >


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.