PHP for file download and multi-file upload

Source: Internet
Author: User
Tags http post php class

File Download:

Html:


PHP Processing:

<?php $filename =$_get[' filename '];//set the download filename header (' content-disposition:attachment;filename= '. BaseName ($ filename); header (' Content-length: ' FileSize ($filename)); ReadFile ($filename);

File Upload:

HTML code:


PHP Code:

common.func.php

<?php/** * Get File extension * @param string $filename * @return string */function getext ($filename) {return Strtolower (PathInfo ( $filename, Pathinfo_extension));} /** * Generates a unique string * @return string */function getuniname () {return MD5 (Uniqid (Microtime (True), true));}

upload.func1.php

<?php/** * Build upload file information * @return unknown */function getFiles () {$i =0;foreach ($_files as $file) {if (is_string ($file [' name ' ]) {$files [$i]= $file; $i + +;} ElseIf (Is_array ($file [' name '])) {foreach ($file [' name '] as $key = + $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

<?php//print_r ($_files); Header ("Content-type:text/html;charset=utf-8"); require_once ' upload.func1.php '; Require_once ' common.func.php '; $files =getfiles ();//Print_r ($files); foreach ($files as $fileInfo) {$res =uploadfile ($ FileInfo); Echo $res [' mes '], ' <br/> '; $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

<?php class upload{protected $fileName;p rotected $maxSize;p rotected $allowMime;p rotected $allowExt;p rotected $ uploadpath;protected $imgFlag;p rotected $fileInfo;p rotected $error;p rotected $ext,/** * @param string $fileName * @param String $uploadPath * @param string $imgFlag * @param number $maxSize * @param array $allowExt * @param array $allowMime */ 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->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_maxThe 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;} /** * Detection Extension * @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->fileinfo[' type '], $this- Allowmime) {$this->error= ' disallowed file type '; return false;} return true;} /** * Detect if it is a real picture * @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 (' <span style= ' color:red ' > '. $this->error. ' </span> ');} /** * 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

<?php header (' Content-type:text/html;charset=utf-8 '); require_once ' upload.class.php '; $upload =new upload (' MyFile1 ', ' Imooc '); $dest = $upload->uploadfile (); Echo $dest;


PHP for file download and multi-file upload

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.