PHP file Upload class application ____php

Source: Internet
Author: User
<?php/** * Steps: * 1. Import inlcude This class file, the following function upload Configuration Storage path * 2. Direct use, with the front desk, run function, notice the foreground form open Enctype * 3. If run into Work, transshipment file to specified directory, get return value URL physical full path * 4. Store the path of the specified storage in the database//** * File Upload function, save the file to the specified path * @param * $fil
        ENAME: The file name written in the form Name= "XXX", will automatically identify the $_files * @return successfully return the picture storage physical full path, failure return false/function upload ($filename) { Include_once (__root__. ') /common/upload.class.php ');//Introducing class file $path = __root__. " /public/upload/". Date (" Y-m-d ", Time ());//define Path $upload = new Upload ($filename, $path);//Instantiate object $url = $upload
        ->uploadfile (),//Call member method, successfully returns the saved physical address if (empty ($url)) {return false;
    return $url; /** * Modify member Information * @param $id which member $data modified value [Array form] * @return successfully returns the number of affected rows; failed return false/function UPDA
            Te_user ($id, $data) {$info = Get_user ($id);//Check out this link information/To determine delete picture operation, empty the image address if (@ $data [' del_pic ']) { Unlink (__root__. ') /public/'. $info [' User_iMG ']);
            unset ($data [' del_pic ']);
        $data [' user_img '] = ""; } if (!empty ($_files[' user_img ' [' name ')]) {@unlink (__root__.)
            /public/". $info [' user_img ']);//delete original file exit first;
            $url = Upload (' user_img ');/move file if ($url = = False) {return false;

        $data [' user_img '] = strstr ($url, "upload");//Assign new address value} $user = M (' Cms_user ');
        $data [' password '] = MD6 ($data [' Password ']);
        $where = "user_id = $id";
        $re = $user->where ($where)->data ($data)->update ();
        echo $link->getlastsql (); exit;
        if ($re) {return true;
    return false; }//--------------------------The application method for class----------------------//------------------------------   Class starts-----------------------------class upload{protected $fileName;  File name protected $maxSize; Maximum file size protected $allowMime; Allowed picture range protected $ALLOWEXT; The allowed file suffix name protected $uploadPath;        Set file upload path protected $imgFlag;   
        Whether to open detection of real image protected $fileInfo; protected $error;//returned file error protected $ext; Gets the file suffix public function __construct ($fileName = ' MyFile ', $uploadPath = './uploads ', $imgFlag =true, $maxSize =5242880,$ Allowext=array (' jpeg ', ' jpg ', ' png ', ' gif '), $allowMime =array (' image/jpeg ', ' image/png ', ' image/gif ')) {$this->fi
            Lename= $fileName;
            $this->maxsize= $maxSize;
            $this->allowmime= $allowMime;
            $this->allowext= $allowExt;
            $this->uploadpath= $uploadPath;
            $this->imgflag= $imgFlag;
        $this->fileinfo=$_files[$this->filename];
            /** * Detect upload File Error * @return Boolean/protected function CheckError () { if (!is_null ($this->fileinfo)) {if ($this->fileinfo[' ERROR ']>0) {switch ($ This-> fileinfo[' ERROR '] {Case 1: $this->error= ' exceeded the PHP configuration file Upload_max_fi
                            Value of the lesize option ';
                        Break
                            Case 2: $this->error= ' exceeds the value set in the form max_file_size ';
                        Break
                            Case 3: $this->error= ' file is partially uploaded ';
                        Break
                            Case 4: $this->error= ' did not choose to upload file ';
                        Break
                            Case 6: $this->error= ' did not find a temporary directory ';
                        Break
                            Case 7: $this->error= ' file is not writable ';
                        Break
                            Case 8: $this->error= ' due to PHP extensions interrupt file Upload ';

                    Break
                return false;}else{return true;
                }else{$this->error= ' error on file ';
            return false; }/** * Detects the size of uploaded files * @return Boolean/protected function checksize ()
                {if ($this->fileinfo[' size ']> $this->maxsize) {$this->error= ' upload file too large ';
            return false;
        return true;
            /** * Detect Extension S * @return Boolean/protected function Checkext () {
            $this->ext=strtolower (pathinfo ($this->fileinfo[' name '],pathinfo_extension));
                if (!in_array ($this->ext, $this->allowext)) {$this->error= ' disallowed extension ';
            return false;
        return true;
            /** * Detection File Type * @return Boolean/protected function Checkmime () { if (!in_array ($this-&Gt;fileinfo[' type '], $this->allowmime)) {$this->error= ' disallowed file type ';
            return false;
        return true;
            /** * Detects if it is a real picture * @return Boolean/protected function checktrueimg () { if ($this->imgflag) {if (! @getimagesize ($this->fileinfo[' Tmp_name ')) {$thi
                    S->error= ' is not a real picture ';
                return false;
            return true; }/** * detects if uploading via HTTP POST * @return Boolean/protected Functio n Checkhttppost () {if (!is_uploaded_file ($this->fileinfo[' Tmp_name ')) {$this->error= ' file is not
                is uploaded through the HTTP POST method ';
            return false;
        return true; /** * Display Error/protected function ShowError () {exit (' <span style= ' color: Red ">". $thIs->error. '
        </span> '); /** * Detect directory does not exist create/protected function Checkuploadpath () {if!file_exists (
            $this->uploadpath)) {mkdir ($this->uploadpath,0777,true));
            /** * produces a unique string * @return string/protected function Getuniname () {
        return MD5 (Uniqid (Microtime (True), true); /** * Upload file * @return String */Public Function UploadFile () {if ($ This->checkerror () && $this->checksize () && $this->checkext () && $this-> Checkmime () && $this->checktrueimg () && $this->checkhttppost ()) {$this-&GT;CHECKUPLOADPA
                Th ();
                $this->uniname= $this->getuniname (); $this->destination= $this->uploadpath. ' /'. $this->uniname. '.
                $this->ext; if (@move_uploaded_file ($this->fileinfo[' Tmp_name '], $this->destination)) {return $this->destination;
                    }else{$this->error= ' file move failed ';
                $this->showerror ();
            }else{$this->showerror (); }
        }
    }

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.