How to access error code associated with file upload in PHP ? Last Updated : 09 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 sizeThe 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 successfully1 => Uploaded file cross the limit2 => 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. index.php Directory : Select file to upload: 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: Comment More infoAdvertise with us Next Article How to access error code associated with file upload in PHP ? R rohanmittal1366 Follow Improve Article Tags : PHP Geeks-Premier-League-2022 PHP-Questions Similar Reads Write a Code To Upload A File in PHP? PHP makes it easy to upload files in web applications, whether it's for images, documents, or other types of files. With its built-in functions, uploading files to the server becomes a simple task. However, it's important to be cautious when allowing file uploads, as they can pose security risks. Al 6 min read How to access actual name of uploaded file in PHP ? In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES["file"]["name"]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded.The file refers to the name which is defined in the "index.html" form in the input of the file.The 2 min read How to upload a file in PHP ? In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart 3 min read How to Log Errors and Warnings into a File in PHP? In PHP, errors and warnings can be logged into a file by using a PHP script and changing the configuration of the php.ini file. Two such approaches are mentioned below: Approach 1: Using error_log() FunctionThe error_log() function can be used to send error messages to a given file. The first argume 3 min read How to check the Type and Size before File Uploading in PHP ? In PHP, it's essential to validate file types and sizes before allowing users to upload files to a server. This helps prevent potential security vulnerabilities and ensures that only allowed file types and sizes are accepted. PHP provides various methods to perform these validations, including check 2 min read Like