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>
<html>
<head>
 
<style>
*{box-sizing:border-box;}
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;}
</style>
<body>
 <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">
</form>
 </div>
</body>
</html>

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.

<?php

echo '<pre>';
var_export($_POST);
echo '</pre>';

echo '<pre>';
var_export($_FILES);
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,
  ),
)

Explanation

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.

UPLOAD_ERR_OK

To check if the file has been uploaded or not.

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_OK)
  echo "Value: 0; There is no error, the file uploaded with success.";

UPLOAD_ERR_INI_SIZE

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.

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_INI_SIZE)
  echo "Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.";

UPLOAD_ERR_FORM_SIZE

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

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_FORM_SIZE)
  echo "Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";

UPLOAD_ERR_PARTIAL

The above code will define the partial upload error.

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_PARTIAL)
  echo "Value: 3; The uploaded file was only partially uploaded.";

UPLOAD_ERR_NO_FILE

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

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_NO_FILE)
  echo "Value: 4; No file was uploaded.";

UPLOAD_ERR_NO_TMP_DIR

checks if a temporary folder is missing

if($_FILES['book_pdf']['error']==UPLOAD_ERR_NO_TMP_DIR)
  echo "Value: 6; Missing a temporary folder.";

UPLOAD_ERR_OK

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

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_OK)
  echo "Value: 7; Failed to write file to disk.";

UPLOAD_ERR_EXTENSION

  if($_FILES['book_pdf']['error']==UPLOAD_ERR_EXTENSION)
  echo "Value: 8; A PHP extension stopped the file upload.";
 

 

Related Articles

Last modified: March 13, 2019

Comments

Write a Reply or Comment

Your email address will not be published.