Php file upload code

Source: Internet
Author: User

[Php]/* upload of a single file function Upload File configuration the size allowed by the suffix configuration to obtain the file suffix to determine the file suffix Error */class UpTool {protected $ allowExt = 'jpg, jpeg, gif, bmp, png '; protected $ maxSize = 1; // 1 M, in the unit of M protected $ file = null; // prepare to store the uploaded file information protected $ errno = 0; // error Code protected $ error = array (0 => 'error ', 1 => 'upload file size exceeds system limited', 2 => 'upload file size exceeds webpage Form', 3 => 'only part of the file is upload ', 4 => 'file not upload', 6 => 'temporary folder not found ', 7 => 'file writing failed', 8 => 'file suffixes not allowed ', 9 => 'file size exceeds the permitted range of the class', 10 => 'Directory creation error Failed ', 11 => 'file movement failed');/* upload */public function up ($ key) {if (! Isset ($ _ FILES [$ key]) {return false;} $ f =$ _ FILES [$ key]; // check whether the upload is successful if ($ f ['error']) {$ this-> errno = $ f ['error']; return false ;} // get the suffix $ ext = $ this-> getExt ($ f ['name']); // check the suffix if (! $ This-> isAllowExt ($ ext) {$ this-> errno = 8; return false;} // check the size if (! $ This-> isAllowSize ($ f ['SIZE']) {$ this-> errno = 9; return false ;} // create a directory $ dir = $ this-> mk_dir (); if ($ dir = false) {$ this-> errno = 10; return fasle ;} // generate a random file name $ newname = $ this-> randName (). '. '. $ ext; // $ dir = $ dir. '/'. $ newname; // move if (! Move_uploaded_file ($ f ['tmp _ name'], $ dir. '/'. $ newname) {$ this-> errno = 11; return false;} return true; // str_replace (ROOT, '', $ dir);} public function getErr () {return $ this-> error [$ this-> errno];}/* parm string $ exts allows suffixes to automatically add allowed suffixes, and file size */public 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 sensitivity issues */protected function isAllowExt ($ ext) {return in_array (strtolower ($ ext), explode (',', strtolower ($ this-> allowExt )));} /* check the file size */protected function isAllowSize ($ size) {return $ size <= $ this-> maxSize * 1024*1024 ;} // method for creating a directory by date protected function mk_dir () {$ dir = 'images /'. date ('ym/D'); if (is_dir ($ dir) | mkdir ($ dir, 0777, true) {return $ dir ;} else {return false ;}/ * generate random file name */protected function randName ($ length = 6) {$ str = 'abcdefghijkmnpqrstuvwxyz23456789 '; return substr (str_shuffle ($ str ), 0, $ length) ;}} form [html] <form action = "up. php "method =" post "enctype =" multipart/form-data "> username: <input type =" text "name =" username "/> <br/> Avatar: <input type = "file" name = "pic"/> <input type = "submit" value = "submit"/> </form> call [php] require ('. /UpTool. class. php '); $ uptool = new UpTool (); $ uptool-> setExt ('rar, doc'); $ uptool-> setSize (1 ); if ($ uptool-> up ('pic ') {echo 'uploaded successfully';} else {echo 'failed'; echo $ uptool-> getErr ();}

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.