PHP File Upload class

Source: Internet
Author: User

<?phpclass Upload {private $error;    Private $destination;    Private $fileInfo;    Private $uploadDir;    Private $maxFileSize;    Private $allowExt;    Private $checkImage; function __construct ($fileInfo, $uploadDir = './uploads ', $maxFileSize = 1024x768 * 1024x768 * 1, $allowEx  t = array (' jpg ', ' gif ', ' PNG ', ' BMP ', ' jpeg '), $checkImage = true, $allowMime = Array (' Image/gif ',        ' Image/jpeg ', ' image/png ') {$this->fileinfo = $fileInfo;        $this->uploaddir = $uploadDir;        $this->maxfilesize = $maxFileSize;        $this->allowext = $allowExt;        $this->checkimage = $checkImage;    $this->allowmime = $allowMime;            /*** * Processing Upload file * @return String */Public Function UploadFile () {if (! $this->fileinfo) {            $this->error = ' File information does not exist ';        return $this->error; if ($this->checkerror () && $this->checkimagetype () && $tHis->checkmaxfilesize () && $this->checkallowext () && $this->checkpostupload () &&am P            $this->checkmime ()) {//Determine if the folder exists $this->checkuploaddir (); Generates a unique file name $destination = $this->uploaddir. ‘/‘ . $this->builduniqueid (). ‘.‘ .            $this->getext (); if (@move_uploaded_file ($this->fileinfo[' tmp_name '), $destination) {$this->destination = $destinati                On            return $this->destination;            } else {$this->error = ' Move upload file failed ';        } return $this->error;    } return $this->error;        } Private Function CheckError () {if ($this->fileinfo[' error '] = = UPLOAD_ERR_OK) {return true; } $this->error = ' upload error, error code: '.        $this->fileinfo[' ERROR '];    return false; } Private Function Builduniqueid () {return MD5 (Uniqid (Microtime (True), true));    } Private Function Checkuploaddir () {if (!file_exists ($this->uploaddir)) {mkdir ($this->up            Loaddir, 0777, true);        chmod ($this->uploaddir, 0777); }} Private Function Checkallowext () {if (In_array ($this->getext (), $this->allowext)) {RET        Urn true;        } $this->error = ' file of this type cannot be uploaded ';    return false;            } Private Function Checkmaxfilesize () {if ($this->maxfilesize > $this->fileinfo[' size ') {        return true;        } $this->error = ' the size of the uploaded file has exceeded the limit ';    return false; } Private Function Checkimagetype () {if ($this->checkimage) {if ($this->checkallowext () &            & GetImageSize ($this->fileinfo[' Tmp_name ')) {return true;            } $this->error = ' upload is not a picture ';        return false;    } return true; } Private Function Checkpostupload () {if (Is_uploaded_file ($this->fileinfo[' Tmp_name ')) {return true;        } $this->error = ' is not a post-uploaded file ';    return false; } Private Function Checkmime () {if (In_array ($this->getmime (), $this->allowmime)) {return Tru        E        } $this->error = ' MIME type incorrect ';    return false; } Private Function Getmime () {if ($imageInfo = getimagesize ($this->fileinfo[' Tmp_name ')) {retur        n $imageInfo [' MIME '];    } return false;    } Private Function Getext () {return strtolower (pathinfo ($this->fileinfo[' name '], pathinfo_extension)); }}

  

PHP File Upload class

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.