While uploading the files or writing the code, developers do many mistakes which we cannot figure out most of the time. Some spelling mistakes, some forgotten parts in the code lead to failed uploading of the file and give warnings or errors. To avoid this kind of mistake, we should learn about the common mistakes occurring for a better understanding.
The following examples help us to understand in a better way.
Example 1: When we forgot to write enctype=”multipart/form-data” in the form field, then it does not allow us to upload a file because this specifies that the form we are submitting has a file type, so it allows the file to upload.
We should not forget to type action=”file_name” in the form field as it is important to specify the form submitting the data, the file it receives the data and executes or retrieve the data accordingly.
index.html
<!DOCTYPE html> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> </ head > < body > < form enctype = "multipart/form-data" action = "file.php" method = "POST" > Choose a file to upload: < input name = "uploadedfile" type = "file" /> < br /> < input type = "submit" value = "Upload File" /> </ form > </ body > </ html > |
file.php
<?php echo 'file count=' , count ( $_FILES ), "\n" ; print "<pre>" ; print_r( $_FILES ); print "</pre>" ; echo "\n" ; ?> |
Output:
Example 2: When we forgot to type method=”POST” in the form field then it is not able to upload because it is important that the method is POST as the POST method is used for submitting the data.
index.html
<!DOCTYPE html> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> </ head > < body > < form enctype = "multipart/form-data" action = "file.php" > Choose a file to upload: < input name = "uploadedfile" type = "file" /> < br /> < input type = "submit" value = "Upload File" /> </ form > </ body > </ html > |
file.php
<?php if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) { echo 'file count=' , count ( $_FILES ), "\n" ; print "<pre>" ; print_r( $_FILES ); print "</pre>" ; echo "\n" ; } else { echo "Method is not POST" ; } ?> |
Output:
Example 3: The following demonstrates the execution when the uploaded file size is bigger than the allowed file size. Use the “index.html” file used in Example 1.
file.php
<?php // Check method is POST if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) { // Check file size. if ( $_FILES [ "uploadedfile" ][ "size" ] > 10000) { echo "Sorry, your file is too large." ; exit ; // stop the PHP script } echo 'file count=' , count ( $_FILES ), "\n" ; print "<pre>" ; print_r( $_FILES ); print "</pre>" ; echo "\n" ; } else { echo "Method is not POST" ; } ?> |
Output:
Example 4: The following code demonstrates the execution when the extension is not matching the allowed extensions. Use the “index.html” file used in Example 1.
file.php
<?php if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) { $target_file = basename ( $_FILES [ "uploadedfile" ][ "name" ]); // Extract the file extension $imageFileType = strtolower ( pathinfo ( $target_file , PATHINFO_EXTENSION)); // Make an array of allowed extensions $extensions = array ( "jpeg" , "jpg" , "png" , "pdf" ); // Check that extension is present in the array or not if (in_array( $imageFileType , $extensions ) === false) { echo "Invalid file extension ..!!" ; exit ; } echo 'file count=' , count ( $_FILES ), "\n" ; print "<pre>" ; print_r( $_FILES ); print "</pre>" ; echo "\n" ; } else { echo "Method is not POST" ; } ?> |
Output: