PHP encapsulated Single file (picture) Upload class Complete Example _php tips

Source: Internet
Author: User
Tags create directory file upload html form

The example in this article describes the single file (picture) upload class that is encapsulated in PHP. Share to everyone for your reference, specific as follows:

<?php//Encapsulation PHP Single file (picture) upload class///Parameter 1: $file file array 5 property values Name,type,size,tmp,error//Parameter 2: File save path $path//Parameter 3: File upload allowed type $ Allow array $allow =array (' image/jpeg ', ' image/jpg ', ' image/png ', ' image/gif ')//Parameter 4: Maximum size allowed for file uploads $size//return value: Returns $
The name of the ImageName file/header (' Content-type:text/html;charset=utf-8 ');
   Class upload{//defines a property that specifically stores the error message public static $error;
    Method of File Transfer public function UploadFile ($file, $size, $path) {if (!is_dir ($path)) {$this->mkpath ($path);
   $allow =array (' image/jpeg ', ' image/jpg ', ' image/png ', ' image/gif ');
     First, determine if the file has been uploaded to the temporary directory if (!is_array ($file)) {Upload:: $error = ' Not a valid file ';
    return false;  //Determine if the file is uploaded to the temp directory successfully switch ($file [' ERROR ']) {case 1:upload:: $error = ' uploaded file over php.ini upload_max_filesize The value of the option limit.
      ';
    return false; Case 2:upload:: $error = ' the size of the upload file exceeds the value specified by the Max_file_size option in the HTML form.
      ';
    return false;
      Case 3:upload:: $error = ' file is only partially uploaded ';
    return false; Case 4:upload:: $error =' No files were uploaded ';
    return false;
      Case 6:upload:: $error = ' No temporary folder found ';
    return false;
       Case 7:upload:: $error = ' file write Failed ';
   return false;
     //Determine if the file type is a picture if (!in_array ($file [' type '], $allow)) {Upload:: $error = ' is not a required file type ';
    return false;
     //Determine whether the file size is within the allowable range if ($file [' Size ']> $size) {Upload:: $error = ' exceeds maximum file size allowed ';
    return false; //File Transfer if (Move_uploaded_file ($file [' Tmp_name '], $path. ')
    /'. $this->getname ($file)) {return $file [' name '];
    }else{return Upload:: $error;
  }//Create directory method Private function Mkpath ($path) {mkdir ($path);
    }//File Rename Private function GetName ($file) {return time (). $file [' name '];

 }
}

More about PHP Interested readers can view the site topics: "PHP file Operation Summary", "PHP graphics and pictures Operating skills summary", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.