Optimized single-File upload function

Source: Internet
Author: User

$fileInfo  = @$_files [' myFile '];//receive form uploaded files//5 parameters, the first one is must, followed by default values upload ($fileInfo, "626800", False, "IMGs", Array ("TXT")); Function upload ($fileInfo, $maxSize =2097152, $flag =true, $path = "Upload", $ Allowedext=array ("JPEG", "JPG", "png", "GIF"))  {//$maxSize  = 2 * 1024 * 1024;  //  upload file size limit within 2M//$flag  = true;//  Judging error number if  ($fileInfo  [' ERROR '] >  0)  {switch  ($fileInfo  [' error ')  {case 1 : $mes  =  ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file '; break;case 2 : $mes  =  ' upload file exceeds the size of the form max_file_size limit ' ; break;case 3&nbsp: $mes  =  ' files are uploaded '; break;case 4 : $mes  =  ' no files selected for upload '; BREAK;CASE 6&NBSP: $mes  =  ' no temporary directory found '; break;case 7 :case 8 : $mes  =   ' system error '; exit  (  $mes  );}   Determine the file type//  $ext =strtolower (End (Explode (".",  $fileInfo [' name ']));  //get the suffix name, or use the following method $EXT&Nbsp;= pathinfo  (  $fileInfo  [' name '], pathinfo_extension );//  $allowedExt  = array  (//  "jpeg",//  "jpg",//  "png",//  "GIF"  // )//if (!is_ Array ($ALLOWEDEXT)) {exit ("The file name extension is not an array");} if  (! in_array  (  $ext,  $allowedExt  ))  {exit  (  "Illegal file type"  );}  print_r (getimagesize ($fileInfo [' tmp_name '));exit;//  determine if it is the true picture file type, if you do not need to do this detection can be set $flag for Falseif   ($flag)  {if  (! getimagesize  (  $fileInfo  [' tmp_name '] ))  {exit   (  "Upload file is not a real picture file"  );}}   Determine file size if  ($fileInfo  [' size '] >  $maxSize)  {exit  (  "Upload file size exceeds this site limit"  );} Whether   is uploading via post, it is necessary to detect the temporary file// var_dump (is_uploaded_file ($fileInfo [' tmp_name '));exit;if  (!  is_uploaded_file  (  $fileInfo  [' tmp_name '] )  {exit  (  "Illegal upload method"  );}   Move files after detection//$path  =  "upload";if (! file_exists  (  $path  ))  {mkdir  (  $path, 0755, true ) chmod   (  $path, 0755 );} $uniqName =md5  ( uniqid  ( microtime  ( true ), true )  )  .   "."  .  $ext $destination  =  $path  .  "/"  .  $uniqName;if  (!  @move_ uploaded_file  (  $fileInfo  [' tmp_name '],  $destination  ))  {//  plus @  is to not display overflow error exit  (  "File upload failed"  );} echo  "File Upload successful! ";}


This article is from a "sense of direction" blog, be sure to keep this source http://itech.blog.51cto.com/192113/1606734

Optimized single-File upload function

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.