Thursday, September 4, 2025
HomeLanguagesHow to access error code associated with file upload in PHP ?

How to access error code associated with file upload in PHP ?

Error in PHP occurs when we do not deal with the cases accordingly. 

There are many errors that occur during the file upload. The errors are as follows

  • The size of the file is greater than the expected size
  • The extension is not correct or we can say the file type is not matching according to the conditions.
  • The directory is missing.
  • The file already exists on the preferred directory.

We can access the error code by inbuilt variable $_FILES[“file”][“error”] where it gives us the error code and they are defined as follows.

  • 0 => ‘File is uploaded successfully
  • 1 => Uploaded file cross the limit
  • 2 => Uploaded file cross the limit which is mentioned in the HTML form.
  • 3 => File is partially uploaded or there is any error in between uploading.
  • 4 => No file was uploaded.
  • 6 => Missing a temporary folder.
  • 7 => Failed to write file to disk.
  • 8 => A PHP extension stopped the uploading process.

Now we understand this by giving the below example.

Example 1: In this example, there are many errors listed. It takes the directory name and file from the local device and then sent through the POST method to “file.php”. Then it checks for the directory creation, if not then it creates one otherwise it goes for extension checking and check for allowed extensions listed in the array (you can add according to the program requirements). It then checks the size of the file and then checks for the file existence.




 


file.php




<!DOCTYPE html>
<?php
  
    $target_dir = $_POST["directory_name"]."/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $name = $_FILES["file"]["name"];
    $type = $_FILES["file"]["type"];
    $size = $_FILES["file"]["size"];
    $flag = 1;
  
    if($_SERVER["REQUEST_METHOD"] == "POST")
   {
      
        if(!empty($_POST["directory_name"]))
        {
          
            if(!is_dir($_POST["directory_name"])) {
          
                echo "Creating Directory ..!!";
                mkdir($_POST["directory_name"]);    
                $flag = 1;            
            }
        }
       else
       {
          
          echo "<b>Error: Specify the directory name...</b>";
          $flag = 0;
          exit;    
       }    
      
    // Check if file was uploaded without errors
    if(isset($_FILES["file"]) && $_FILES["file"]["error"] == 0)
    {
        $allowed_ext = array("jpg" => "image/jpg",
                            "jpeg" => "image/jpeg",
                            "png" => "image/png");
      
        // Verify file extension
        $ext = pathinfo($name, PATHINFO_EXTENSION);
  
        if (!array_key_exists($ext, $allowed_ext))    
        {
            die("<b>Error: Please select a valid file format.</b>");
        }    
              
        $maxsize = 200000;
          
        if ($size > $maxsize)    
        {
            die("<b>Error: ". $_FILES["file"]["error"] . 
            "  File size is larger than the allowed limit.</b>");
        }    
                      
      
        // Verify MYME type of the file
        if (in_array($type, $allowed_ext))
        {
            // Check whether file exists before uploading it
            if (file_exists("$target_dir/".$_FILES["file"]["name"]))    
            {
                echo "<b>".$_FILES["file"]["name"]." is already exists.</b>";
            }        
            else
            {
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
                      
                    echo "<b>The "$_FILES["file"]["name"]. " has been uploaded.</b>";
  
                
                else 
                {
                    echo "<b>Error : ". $_FILES["file"]["error"] .
                    " Sorry, there was an error uploading your file.";
                }
            }
        }
        else
        {
            echo "<b>Error: ". $_FILES["file"]["error"] . " Please try again.</b>";
        }
    }
    else
    {
        echo "<b>Error: ". $_FILES["file"]["error"] . " File is not uploaded</b>";
    }
  }
?>
</body>
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS