PHP File Upload

Source: Internet
Author: User
Tags php file upload

<?PHP$allowtype=Array("GIF", "png", "JPG");//set the upload type to GIF, PNG, and JPG    $size= 1000000;//set the file size to 1M (1000000 bytes)    $path= "./uploads";//set the path to save files after uploading//determine if the file can be successfully uploaded to the server, $_files[' myfile ' [' Error '] is 0 for upload success    if($_files[' MyFile '] [' ERROR '] > 0) {              Echo' Upload error: '; Switch($_files[' MyFile '] [' Error ']) {              Case1: die(' Upload file size exceeds the agreed value in PHP config file: upload_max_filesize '));  Case2: die(' Upload file size exceeds the contract value in the form: Max_file_size '));  Case3: die(' files are only partially uploaded ');  Case4: die(' No files uploaded '); default: die(' Last known error '); }    }        //determine if the uploaded file is a file type that is not allowed, through the file's suffix name    $hz=Array_pop(Explode(".",$_files[' MyFile '] [' Name '])); //Determine whether a file is a file type that is allowed to be uploaded by judging the file suffix method    if(!In_array($hz,$allowtype)) {         die("This suffix is <b>{$hz}</b&gt, not a permitted file type! "); }        /*You can also restrict the type of file upload by obtaining the main type and subtype in the MIME type of the uploaded file list ($maintype, $subtype) =explode ("/", $_files[' myfile ' [' type ']); if ($maintype = = "text") {//The text file cannot be uploaded by the master type limit, such as. txt. html. PHP and other files die (' problem: cannot upload text files.    ‘); } */    //determine if the uploaded file is size    if($_files[' MyFile '] [' Size '] >$size ) {         die("More than a <b>{$size}</b> byte Size "); }        //For system security, also for files with the same name will not be overwritten, after uploading the file name using the system definition    $filename=Date("Ymdhis").Rand(100,999). ".".$hz; //determine whether to upload files    if(Is_uploaded_file($_files[' MyFile '] [' Tmp_name '])) {          if(!Move_uploaded_file($_files[' MyFile '] [' Tmp_name '],$path.‘ /‘.$filename)) {               die(' Problem: The file cannot be moved to the specified directory. ‘); }     }Else{         die("Problem: uploading a file {$_files[' MyFile '] [' Name ']} is not a legal document: "); }    //output If file upload is successful    EchoFile$upfileUploaded successfully, saved in directory {$path}, the size is {$_files[' MyFile '] [' Size ']} Bytes ";

PHP File Upload

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.