PHP implementation file download with multi-file upload

Source: Internet
Author: User
PHP for file download and multi-file upload

File Download:

Html:

    Download 1.rar    
Download 1.jpg
Download 1.jpg via program
Download nv.jpg

PHP Processing:

 
File Upload:

HTML code:

    

PHP Code:

common.func.php

  
upload.func1.php

   $val) {$files [$i] [' name ']= $file [' name '] [$key]; $files [$i] [' type ']= $file [' type '] [$key]; $files [$i] [' Tmp_name ']=$ file[' Tmp_name ' [$key]; $files [$i] [' ERROR ']= $file [' Error '] [$key]; $files [$i] [' Size ']= $file [' Size '] [$key]; $i + +;}}} return $files;} /** * For single-file, multiple single-file, multiple-file uploads * @param array $fileInfo * @param string $path * @param string $flag * @param number $maxSize * @param array $allowExt * @return string */function uploadfile ($fileInfo, $path = './uploads ', $flag =true, $maxSize =1048576 , $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif ')) {//$flag =true;//$allowExt =array (' jpeg ', ' jpg ', ' gif ', ' png ');//$ maxsize=1048576;//1m//Judgment Error number if ($fileInfo [' Error ']===upload_err_ok) {//Detect upload to get small if ($fileInfo [' Size ']> $maxSize) {$ res[' mes ']= $fileInfo [' name ']. ' Upload file too large ';} $ext =getext ($fileInfo [' name ']);//detects the file type of the uploaded file if (!in_array ($ext, $allowExt)) {$res [' mes ']= $fileInfo [' name ']. ' illegal file type ';} Detects if the picture type is true if ($flag) {if (!getimagesize ($fileInfo [' tmp_name ')]) {$res [' mes ']= $fileInfo [' name ']. ' Not a real picture type ';}} Detects if the file is uploaded via HTTP post (!is_uploaded_file ($fileInfo[' Tmp_name ') {$res [' mes ']= $fileInfo [' name ']. ' The file is not uploaded via HTTP post; if ($res) return $res;//$path = './uploads '; if (!file_exists ($path)) {mkdir ($path, 0777,true); chmod ($path, 0777);} $uniName =getuniname (); $destination = $path. ' /'. $uniName. '. $ext, if (!move_uploaded_file ($fileInfo [' Tmp_name '], $destination) {$res [' mes ']= $fileInfo [' name ']. File move failed ';} $res [' mes ']= $fileInfo [' name ']. ' Upload success '; $res [' Dest ']= $destination; return $res;} else{//Match error message switch ($fileInfo [' ERROR ']) {Case 1: $res [' mes '] = ' upload file exceeds the value of upload_max_filesize option in PHP config file '; Break;case 2 : $res [' mes '] = ' exceeds the size of the form max_file_size limit '; Break;case 3: $res [' mes '] = ' file part is uploaded '; break;case 4: $res [' mes '] = ' no option to upload file '; Break;case 6: $res [' mes '] = ' No temporary directory found '; break;case 7:case 8: $res [' mes '] = ' system error '; return $res;}}

doaction5.php

 
    '; $uploadFiles []= $res [' dest '];} $uploadFiles =array_values (Array_filter ($uploadFiles));p Rint_r ($uploadFiles);

The above is implemented by the function, the download package becomes the class:

Html:


upload.class.php

   Filename= $fileName, $this->maxsize= $maxSize, $this->allowmime= $allowMime; $this->allowext= $allowExt; $ This->uploadpath= $uploadPath, $this->imgflag= $imgFlag $this->fileinfo=$_files[$this->filename];} /** * Detect error Uploading File * @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 config file upload_max_ The value of the FileSize option '; Break;case 2: $this->error= ' exceeds the value max_file_size set in the form '; Break;case 3: $this->error= ' file is partially uploaded '; Break;case 4: $this->error= ' no option to upload files '; Break;case 6: $this->error= ' no temporary directory found '; Break;case 7: $this->error= ' File not writable '; Break;case 8: $this->error= ' due to PHP extension interrupt file Upload '; return false;} Else{return true;}} else{$this->error= ' file error '; return false;}} /** * Detects the size of the uploaded file * @return boolean */protected function Checksize () {if ($this->fileinfo[' size ']> $this->maxsize) {$this->error= ' upload file too large '; return false;} return true;} /** * Detect Extension * @return Boolean */protected function Checkext () {$this->ext=strtolower (pathinfo ($this->fileinfo[' name '],pathinfo_extension)); 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->fileinfo[' type '], $this- Allowmime) {$this->error= ' disallowed file type '; return false;} return true;} /** * Detects if the image is real * @return Boolean */protected function checktrueimg () {if ($this->imgflag) {if ([email protected] ( $this->fileinfo[' Tmp_name ')) {$this->error= ' is not a real picture '; return false;} return true;}} /** * detects if an HTTP post is uploaded to the * @return Boolean */protected function Checkhttppost () {if (!is_uploaded_file ($this- fileinfo[' Tmp_name ') {$this->error= ' file is not uploaded via HTTP post '; return false;} return true;} /** * Display Error */protected function ShowError () {exit (''. $this->error. '');} /** * Detection directory does not exist create */protected function Checkuploadpath () {if (!file_exists ($this->uploadpath)) {mkdir ($this, uploadpath,0777,true);}} /** * Generates 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->checkuploadpath (); $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 ();}}}

doaction6.php

 
    UploadFile (); Echo $dest;


  • 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.