PHP File upload Single File upload

Source: Internet
Author: User
Tags http post php file upload
For the sake of simplicity, PHP files are written in a file with the form form.

PHP Single File upload---->

1 2 3 4     
 
  
 5 6 7  A  -  - the - - 
 Php -if(!Empty($_files)){ +Header(' Content-type:text/html;charset=utf-8 '); -$fileInfo=$_files[' MyFile ']; +Print_r($_files); A//Exit and print the error message if an upload error occurs atif($fileInfo[' Error ']>0){ -Switch($fileInfo[' Error ']){ - Case1: -$msg _error= ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file '; - Break; - Case2: in$msg _error= ' exceeds the size of the form max_file_size limit '; - Break; to Case3: +$msg _error= ' File partial upload '; - Break; the Case4: *$msg _error= ' No file Upload '; $ Break;Panax Notoginseng Case6: -$msg _error= ' No temp directory found '; the Break; + Case7: A Case8: the$msg _error= ' System error '; + Break; -            } $Exit($msg _error); $        } -$filename=$fileInfo[' Name ']; -//gets the file name extension the$ext=Strtolower(substr($filename,Strrpos($filename,'.') +1)); -//define an extension that allows uploadingWuyi$allowExt=Array(' txt ', ' HTML ', ' PNG ', ' gif ', ' JPEG '); the//detecting the type of upload file -if(!In_array($ext,$allowExt)){ WuExit(' Upload file type error '); -        } About $ -//detecting the size of a file -$maxSize=2097152; -if($fileInfo[' Size ']>$maxSize){ AExit(' Upload file too large '); +        } the -//detects if an HTTP post is being uploaded $if(!Is_uploaded_file($fileInfo[' Tmp_name '])){ theExit(' file is not submitted via HTTP POST. ')); the        } the the//ensure file name is unique and prevent overwriting of files of the same name -$uniqName=MD5(uniqid(Microtime(true),true)).'.'.$ext; in the//defines the folder under which to save, if no folder is created the$path= ' uploads '; Aboutif(!file_exists($path)){ themkdir($path, 0777,true); thechmod($path, 0777); the        } +$destination=$path.' /'.$uniqName; - the//move files to the directory you want to saveBayiif(! @Move_uploaded_file($fileInfo[' Tmp_name '],$destination)){ theExit(' File upload failed '); the        } - -Echo' Upload success '; the the    } the?>

The above describes the php file upload of a single file upload, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.