PHP File Upload class Code _php tutorial

Source: Internet
Author: User
Tags php file upload
[PHP]/* Single file upload feature upload file configuration allowed suffix configuration allowed size get file suffix judge file suffix error */class uptool{protected $allowExt = ' jpg,jpeg,gif,bmp,png '; protected $maxSize = 1; 1M, protected $file = NULL in M units; Prepare to store upload file information protected $errno = 0; Error code protected $error = Array (0=> ' no error ', 1=> ' upload file size exceeds system limit ', 2=> ' upload file size exceeds Web Form limit ', 3=> ' file only partially uploaded ', 4=> ' No files are uploaded ', 6=> ' cannot find Temp folder ', 7=> ' file write Failed ', 8=> ' disallowed file suffix ', 9=> ' file size exceeds the allowable range of class ', 10=> ' Create directory failed ', 11=> ' file move failed ' ); /* Upload */Public function up ($key) {if (!isset ($_files[$key])) {return false;} $f = $_files[$key];//Verify that the upload is successful if ($f [' ER Ror ']) {$this->errno = $f [' ERROR ']; return false;}//get suffix $ext = $this->getext ($f [' name ']); Check the suffix if (! $this->isallowext ($ext)) {$this->errno = 8; return false;}//Check size if (! $this->isallowsize ($f [' size ') ]) {$this->errno = 9; return false;}//Create directory $dir = $this->mk_dir (); if ($dir = = False) {$this->errno = ten; return fasle;}//Generate random file name $newname = $this->randname (). '. '. $ext; $dir = $Dir. '/'. $newname; Move if (!move_uploaded_file ($f [' tmp_name '), $dir. '/'. $newname)} {$this->errno = one; return false;} return True;//str_replace (ROOT, ", $dir); The Public Function Geterr () {return $this->error[$this->errno];}/* Parm string $exts allowed suffixes to automatically add the allowed suffixes, and the size of the file */pub Lic function Setext ($exts) {$this->allowext = $exts;} public Function SetSize ($num) {$this->maxsize = $num;}/* String $file return string $ext suffix */protected function Getext ($file) {$tmp = explode ('. ', $file); return end ($TMP);} * String $ext file suffix return bool prevents case-sensitive issues */protected function Isallowext ($ext) {return In_array (Strtolower ($ext), explode (', ', Strtolower ($this->allowext))) ; }/* Check the size of the file */protected function isallowsize ($size) {return $size <= $this->maxsize *1024*1024;}//method to create a directory by date PR otected function Mk_dir () {$dir = ' images/'. Date (' ym/d '); if (Is_dir ($dir) | | mkdir ($DIR, 0777,true)) {return $dir;} el SE {return false;}} /* Generate random file name */protected functionRandname ($length = 6) {$str = ' abcdefghijkmnpqrstuvwxyz23456789 '; return substr (Str_shuffle ($STR), 0, $length);}} Form form [HTML] Another page called [PHP] require ('./uptool.class.php '); $uptool = new Uptool (); $uptool->setext (' Rar,doc '); $uptool->setsize (1); if ($uptool->up (' pic ')) {echo ' upload succeeded ';} else {echo ' failed '; Echo $uptool->geterr ();}

http://www.bkjia.com/PHPjc/477843.html www.bkjia.com true http://www.bkjia.com/PHPjc/477843.html techarticle [PHP]/* Single file upload feature upload file configuration allowed suffix configuration allowed size get file suffix judge file suffix error */class uptool{protected $allowExt = JP ...

  • Related Article

    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.