Upload Image function

Source: Internet
Author: User

1<?PHP2     /**3 * File Upload4 * @param array $file file upload information (is an array with 5 elements)5 * @param array $allow The type of file upload allowed6 * @param string & $error reference passed to log error messages7 * Upload directory @param string $path file8 * @param int $maxsize = 1024*1024 Allow file Upload size9 * @param mixed false/$newname If the upload fails, return false, and the new name of the file will be returned successfully.Ten      */ One      A     functionUpload$file,$allow,&$error,$path,$maxsize=1048576){ -             Switch($file[' Error ']){ -                  Case1:$error= ' The uploaded file exceeds the value of the option limit! '; the                          return false; -                  Case2:$error= ' the size of the uploaded file exceeds the value specified by the option! '; -                          return false; -                  Case5 |$error= ' files are only partially uploaded! '; +                          return false; -                   Case4:$error= ' No file selected ' was uploaded! '; +                          return false; A                  Case6: at                  Case: W$error= ' File write failed, System busy! '; -                          return false; -                 } -  -             //determine the size of a file -             if($file[' Size '] >$maxsize){ in                 $error= ' File upload too big! '; -                 return false; to             } +  -             if(!In_array($file[' type '],$allow)){ the                 $error= ' File upload type does not exist, allow upload is type: '.implode(‘,‘,$allow); *                 return false; $             }Panax Notoginseng  -             $newname= Randname ($file[' Name ']); the             $target=$path. ‘/‘ .$newname; +             $result=Move_uploaded_file($file[' Tmp_name '],$target); A             if($result){ the                 return $newname; +}Else{ -                 $error= ' An unknown error occurred and the upload failed! ‘; $                 return false; $             } -         } -  the     /** - * Generate a random name function, file name = Current time plus random numberWuyi * @param string $filename file original name the * @return A new name for string $newname file -      */ Wu     functionRandname ($filename){ -         $newname=Date(' Ymdhis '); About         //random numbers that follow $         $str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -          for($i= 0;$i< 6;$i++){ -             $newname.=$str[Mt_rand(0,strlen($str)-1)]; -         } A         $newname.=STRRCHR($filename,‘.‘); +         return $newname; the}

Upload Image 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.