Understand the File Upload Error Constants in PHP

Uploading files or images can be a crucial part of any web application. Let’s say you ask a user to upload his picture in jpeg or png format.

So how you are going to check if the file is being uploaded successfully? What if the file was uploaded partially due to some network problem?

PHP has many built-in functions to facilitate the file uploading process. PHP also has pre-defined error constants which help to check if the file was uploaded successfully, the size of the file being uploaded is within the allowed upload size etc.

In this tutorial, we will learn such error constants. We will develop a real-world example of how to upload a file and use its error constants to verify and validate the file and the upload process.

PHP File Upload Error Constants

First, create a web form as shown below.

<!DOCTYPE html>
body{font-family: sans-serif; color:#222; font-size:14px; background-color:#dbdbdb;}
.form_box{width:378px; padding:8px;}
label{width:200px; }
input{padding:10px; width:100%;margin-bottom:13px;}
input[type="submit"]{border:none; outlin:none; background-color:#44d6b6; color:white;}
 <div class="form_box">
<form class="" action="practice_ac.php" method="post" enctype="multipart/form-data">
  <label for="">Book Title</label>
  <input type="text" name="book_name" value="" id="book_name"><br/>
  <label for="">PDF Copy</label>
  <input type="file" name="book_pdf" value="" id="book_pdf">
  <input type="submit" name="submit" value="Upload PDF">

Now create a new file, practice_ac.php and copy paste the following code. To check the $_POST and $_FILES arrays, use the following code.


echo '<pre>';
echo '</pre>';

echo '<pre>';
echo '</pre>';


The output will be something like this if everything goes okay.

array (
  'book_name' => 'PHP Visual Guide',
  'submit' => 'Upload PDF',
array (
  'book_pdf' => 
  array (
    'name' => 'PHP visual Guide.pdf',
    'type' => 'application/pdf',
    'tmp_name' => 'C:\wamp64\tmp\php3FC4.tmp',
    'error' => 0,
    'size' => 1741488,


The code above implies that an array called $_FILES has been populated. It is a global array which contains details of the uploaded file.

$_FILES[‘book_pdf’][‘name’] contains the name of the file being uploaded
$_FILES[‘book_pdf’][‘error’] contains the error code.

Using Error Constants

PHP has constant values for file upload errors which lets you specify the exact error message if an error occurs in uploading.


To check if the file has been uploaded or not.

  echo "Value: 0; There is no error, the file uploaded with success.";


The above code will return error code one if the size of the uploaded file is more than the limit set via upload_max_filesize directive in php.ini.

  echo "Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.";


check if the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

  echo "Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";


The above code will define the partial upload error.

  echo "Value: 3; The uploaded file was only partially uploaded.";


This error constant is useful to check if there was any file to upload or not.

  echo "Value: 4; No file was uploaded.";


checks if a temporary folder is missing

  echo "Value: 6; Missing a temporary folder.";


The error is thrown when writing the file to disk has failed.

  echo "Value: 7; Failed to write file to disk.";


  echo "Value: 8; A PHP extension stopped the file upload.";


Related Articles

Leave a Reply