File upload and download learning notes (2)---Multi-file, single-file, multiple single-file function encapsulation

Source: Internet
Author: User

One: Leading knowledge points:

1:is_string () Determines whether a variable is a string Is_array determines whether a variable is an array

The 2:pathinfo () function can get the file extension.

3: Create a directory function mkdir ()

4: Generate a unique random number uniqid ()

5: Determine if files, directories exist File_exsits ()

Second: Ideas and processes

Three: Encapsulated function code

1<?PHP2 /**3 * Get the desired file type4 * @return Unknown5  */6 functionGetFiles () {7     $i= 0;8     foreach($_files  as $file ) {9         //Judging is a single fileTen         if(is_string($file[' Name '] )) { One             $files[$i] =$file; A             $i++; -         //Judging is multiple files -}ElseIf(Is_array($file[' Name '] )) { the             foreach($file[' Name '] as $k=$v ) { -                 $files[$k] [' name '] =$v; -                 $files[$k] [' type '] =$file[' type '] [$k]; -                 $files[$k] [' tmp_name '] =$file[' Tmp_name '] [$k]; +                 $files[$k] [' ERROR '] =$file[' ERROR '] [$k]; -                 $files[$k] [' size '] =$file[' Size '] [$k]; +             } A         } at     } -     return $files; - } - /** - * Upload Files - * @param unknown $fileInfo in * @return String -  */ to functionUploadfiles ($fileInfo,$path= "TTT",$allowExt=Array(' jpg ', ' jpeg ', ' png ', ' gif '),$maxSize= 20000000,$flag=false) { +     //get the file name extension via PathInfo -     $ext=PathInfo($fileInfo[' Name '],pathinfo_extension); the     //get a unique name for a file *     $uniName=MD5(uniqid(Microtime(true),true) ) . ‘.‘ .$ext; $     $destination=$path. ‘/‘ .$uniName;Panax Notoginseng     $res[' mes '] =NULL; -     //If the directory does not exist, create a directory the     if(!file_exists($path )) { +         mkdir($path, 0777,true ); A     } the     //Judging the error number +     if($fileInfo[' error '] = = 0) { -         //Determine file size $         if($fileInfo[' Size '] >$maxSize) { $             $res[' mes '] =$fileInfo[' name ']. " Upload file too large "; -         } -         //determine file type the         if(!In_array($ext,$allowExt )) { -             $res[' mes '] =$fileInfo[' name ']. " File format is incorrect ";Wuyi         } the         //determine if the picture is true -         if($flag) { Wu             if(!getimagesize($fileInfo[' Tmp_name '] )) { -                 $res[' mes '] =$fileInfo[' name ']. " Not a real picture "; About             } $         } -         //determine whether to upload via HTTP POST -         if(!Is_uploaded_file($fileInfo[' Tmp_name '] )) { -             $res[' mes '] =$fileInfo[' name ']. " File upload path is incorrect "; A         } +         //If there is a value, end the return the         if($res[' Mes ']) -             return $res; $             //Moving Files the         if(Move_uploaded_file($fileInfo[' Tmp_name '],$destination )) { the             $res[' mes '] =$fileInfo[' name ']. " File Upload Successful "; the             $res[' DST '] =$destination; the         } -         return $res; in}Else { the         Switch($fileInfo[' Error ']) { the              Case1: About                 $res[' Mes ']=$fileInfo[' name ']. ' The upload file exceeds the value of the upload_max_filesize option '; the                  Break; the              Case2: the                 $res[' Mes ']=$fileInfo[' name ']. ' exceeds the size of the form max_file_size limit '; +                  Break; -              Case5 | the                 $res[' Mes ']=$fileInfo[' name ']. ' File Upload not complete ';Bayi                  Break; the              Case4: the                 $res[' Mes ']=$fileInfo[' name ']. ' No option to upload file '; -                  Break; -              Case6: the                 $res[' Mes ']=$fileInfo[' name ']. ' No temp directory found '; the                  Break; the              Case7: the                 $res[' Mes ']=$fileInfo[' name ']. ' File write Failed '; -                  Break; the              Case: X the                 $res[' Mes ']=$fileInfo[' name ']. ' File upload interrupted by php extension '; the                  Break;94         } the         return $res; the     } the}

File upload and download learning notes (2)---Multi-file, single-file, multiple single-file function encapsulation

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.