Home >Backend Development >PHP Tutorial >Understand how PHP file upload works

Understand how PHP file upload works

WBOY
WBOYOriginal
2016-07-25 09:04:29983browse
  1. if(is_uploaded_file($_FILES['myFile']['tmp_name'])){

  2. $upfile = $_FILES['upload' ];
  3. $name = $upfile['name'];
  4. $type = $upfile['type'];
  5. $size = $upfile['size'];
  6. $tmp_name = $upfile['tmp_name'];
  7. $error = $upfile['error'];
  8. switch($type){
  9. case 'image/pjpeg' : $ok=1;
  10. break
  11. }
  12. if($ok){
  13. move_uploaded_file($tmp_name,' up/'.$name);
  14. }else{
  15. echo "File type not allowed";
  16. }
  17. }
  18. ?>

Copy code

Principle analysis: //Form uploads can only use multipart/form-data encoding format $_FILES system function; $_FILES['myFile']['name']File name $_FILES['myFile']['type'] file type, restricted by the server image/** image/x-png application/x-zip-compressed $_FILES['myFile']['size']Upload file size $_FILES['myFile']['tmp_name'] saves the temporary file name after uploading the service $_FILES['myFile']['error'] error code; 0 Success 1 Exceeded php.ini size 2 Exceeded the value specified by the MAX_FILE_SIZE option 3Only partial upload 5Uploaded file size is 0

move_uploaded_file (temporary file, target location and file name); Function to move files to the target location after uploading is_uploaded_file(MIME); Function to determine uploaded MIME type of file



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn