Encapsulation of PHP Single file, multiple single file, multi-file upload function

Source: Internet
Author: User
Tags http post
Form://s.php//to choose to upload a file when you can select more than one file at a time, then add multiple= "multiple", but also pay attention to Name= "MyFile1" and Name= "myfile[" The difference, single file, multi-file upload.
 
  
 <title>Untitled Document</title>Package for upload function://ss.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//The default allows uploading of files only for picture types, and only these picture types: $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), and check if the uploaded file is a real picture $ flag=true//Default upload Saved folder is local ' uploads ' folder, allow upload file size up to 2Mfunction uploadfile ($fileInfo, $path = './uploads ', $flag =true, $ALLOWEXT =array (' jpeg ', ' jpg ', ' png ', ' gif '), $maxSize =2097152) {//Judgment error number if ($fileInfo [' Error ']===upload_err_ok) {// Detects the size of the upload file 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; If the error message is not displayed, return $res with if (@ $res);//$path = './uploads ';//If this folder is not present, create an if (!file_exists ($path)) {mkdir ($path, 0777,true); chmod ($path, 0777);} New file name unique $uniname=getuniname (); $destination = $path. ' /'. $uniName. '. $ext; the//@ symbol is used to not allow the client to see the error message, or to delete the 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{//matching error message//NOTE! The error message does not have 5switch ($fileInfo [' ERROR ']) {Case 1: $res [' mes '] = ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file '; Break;case 2: $res [ ' mes ' = ' exceeds the size of the HTML 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: $res [' mes '] = ' file write failed '; Break;case 8: $res [' mes '] = ' uploaded file interrupted by php extension '; break;} return $res;}}? >//common.ss.php
 Post-upload file operation:
 '; $uploadFiles []= $res [' dest '];//if the error message is not displayed, use @ $uploadFiles []= $res [' dest '];} $uploadFiles =array_values (Array_filter ($uploadFiles));//This makes it easy to save to the database Print_r ($uploadFiles);//Print to view the results of uploading and saving?>
  • 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.