Php implements file download and multifile Upload

Source: Internet
Author: User
Tags unsupported
Php implements file download and multifile Upload file download:

Html:

Download 1.rar
Download 1.jpg
Download 1.jpg through the 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 ;} /*** Upload a single file, multiple single files, and multiple files * @ param array $ fileInfo * @ param string $ path * @ param string $ flag * @ param number $ maxSize * @ param array $ allowExt * @ return string */fun Ction uploadFile ($ fileInfo, $ path = '. /uploads ', $ flag = true, $ maxSize = 1048576, $ allowExt = array ('jpeg', 'jpg ', 'PNG', 'GIF ')) {// $ flag = true; // $ allowExt = array ('jpeg ', 'jpg', 'GIF', 'PNG '); // $ maxSize = 1048576; // 1 M // Determine the error code if ($ fileInfo ['error'] === UPLOAD_ERR_ OK) {// detect if ($ fileInfo ['size']> $ maxSize) {$ res ['Mes '] = $ fileInfo ['name'] after uploading. 'upload file too large ';} $ ext = getExt ($ fileInfo ['name']); // checks the file type of the uploaded file if (! In_array ($ ext, $ allowExt) {$ res ['Mes '] = $ fileInfo ['name']. 'invalid file type';} // checks whether the image type is true if ($ flag) {if (! Getimagesize ($ fileInfo ['tmp _ name']) {$ res ['Mes '] = $ fileInfo ['name']. 'Not real Image type';} // checks whether the object is uploaded through http post. if (! Is_uploaded_file ($ fileInfo ['tmp _ name']) {$ res ['Mes '] = $ fileInfo ['name']. 'file not uploaded through 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 moving failed';} $ res ['Mes '] = $ fileInfo ['name']. 'uploaded successfully'; $ res ['dest'] = $ destination; return $ res;} else {// Match error message switch ($ fileInfo ['error']) {case 1: $ res ['Mes '] = 'the uploaded file exceeds the value of the upload_max_filesize option in the PHP configuration file'; break; case 2: $ res ['Mes '] = 'size beyond the size limit of form MAX_FILE_SIZE'; break; case 3: $ res ['Mes '] = 'upload part of file'; break; case 4: $ res ['Mes '] = 'file not selected'; break; case 6: $ res ['Mes'] = 'no temporary directory found '; break; case 7: case 8: $ res ['Mes '] = 'system error'; break;} return $ res ;}}

DoAction5.php

   ';$uploadFiles[]=$res['dest'];}$uploadFiles=array_values(array_filter($uploadFiles));print_r($uploadFiles);

The above is implemented through functions and downloaded and encapsulated into a 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];} /*** check whether an error occurred while uploading the file * @ return boolean */protected function checkError () {if (! Is_null ($ this-> fileInfo) {if ($ this-> fileInfo ['error']> 0) {switch ($ this-> fileInfo ['error']) {case 1: $ this-> error = 'exceeds the value of the upload_max_filesize option in the PHP configuration file '; break; case 2: $ this-> error = 'exceeds the value set by MAX_FILE_SIZE in the form '; break; case 3: $ this-> error = 'File partially upload'; break; case 4: $ this-> error = 'upload file not selected'; break; case 6: $ this-> error = 'temporary directory not found '; break; case 7: $ this-> error = 'File writable '; break; case 8: $ this-> error = 'File Upload interrupted due to PHP extensions'; break;} return False;} else {return true ;}} else {$ this-> error = 'File Upload error '; return false ;}} /*** check the size of the uploaded file * @ return boolean */protected function checkSize () {if ($ this-> fileInfo ['size']> $ this-> maxSize) {$ this-> error = 'upload file is too large '; return false;} return true;}/*** check extension * @ return boolean */protected function checkExt () {$ this-> ext = strtolower (pathinfo ($ this-> fileInfo ['name'], PATHINFO_EXTENSION); if (! In_array ($ this-> ext, $ this-> allowExt) {$ this-> error = 'unsupported extension '; return false;} return true ;} /*** check file type ** @ return boolean */protected function checkMime () {if (! In_array ($ this-> fileInfo ['type'], $ this-> allowMime) {$ this-> error = 'unsupported file type '; return false ;} return true;}/*** check whether the image is real * @ return boolean */protected function checkTrueImg () {if ($ this-> imgFlag) {if (! @ Getimagesize ($ this-> fileInfo ['tmp _ name']) {$ this-> error = 'not a real image'; return false;} return true ;}} /*** check whether the * @ return boolean */protected function checkHTTPPost () {if (! Is_uploaded_file ($ this-> fileInfo ['tmp _ name']) {$ this-> error = 'file not uploaded through http post '; return false ;} return true;}/*** display error */protected function showError () {exit (''. $ this-> error. '');}/*** create */protected function checkUploadPath () {if (! File_exists ($ this-> uploadPath) {mkdir ($ this-> uploadPath, 0777, true );}} /*** generate 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 movement 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.