Detect http file upload errors in PHP

Errors that occur during file upload create an error code in $_FILES[$field]['error']. Possible error code values are:

  • UPLOAD_ERR_OK (0) – There is no error, the file uploaded with success.
  • UPLOAD_ERR_INI_SIZE (1) – The uploaded file exceeds the upload_max_filesize directive in php.ini.
  • UPLOAD_ERR_FORM_SIZE (2) – The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
  • UPLOAD_ERR_PARTIAL (3) – The uploaded file was only partially uploaded.
  • UPLOAD_ERR_NO_FILE (4) – No file was uploaded.
  • UPLOAD_ERR_NO_TMP_DIR (5) – Missing a temporary folder.
<?php
  $file = '/tmp/w3image.gif';
  $error = $_FILES['w3img']['error'];
  $tmp_name = $_FILES['w3img']['tmp_name'];
  print("<pre>\n");
  if ($error==UPLOAD_ERR_OK) {
    move_uploaded_file($tmp_name, $file);
    print("File uploaded.\n");
  } else if ($error==UPLOAD_ERR_NO_FILE) {
    print("No files specified.\n");
  } else {
    print("Upload failed.\n");
  }
  print("\n");
?>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker