File Uploading concept in PHP

File Uploading concept in PHP
File Uploading concept in PHP

Topic – 50
File Uploading concept in PHP
Part – 5



-We have seen how to restrict the file uploading with type safety.
-Also we have seen how to restrict the file uploading with file size.

-Here we are going to restrict the uploading with type safety and with size limit.
-Let us see the following program which restricts the file from both type safety and limit of the file size.
-Here we have taken the program from the last parts and made some changes in the code.
-When the file is an image of type jpeg or jpg and the file size is less than 100000 bytes then file is going to be uploaded. If any of the condition is not satisfied then the file should not be uploaded.

$name = $_FILES[‘file’][‘name’];
$type = $_FILES[‘file’][‘type’];
$size = $_FILES[‘file’][‘size’];
$max_size = 100000;
$tmp_name = $_FILES[‘file’][‘tmp_name’];
$extension = strtolower(substr($name,strpos($name,’.’)+ 1));
if( isset($name) && !empty($name) ){
if(($extension ==’jpg’|| $extension ==’jpeg’) && $size < $max_size){
$location = “upload/”;
echo “File Uploaded Successfully..!!”;
echo “File Uploading failed..!!!”;
echo “<br>File must be jpg or jpeg type and file size must be less than $max_size”;
else {
echo “Please Select a File”;
<form action=”upload.php” method=”post” enctype=”multipart/form-data” >
<br><br><input type=”file” name=”file”/><br><br>
<input type=”submit” value=”upload” />

Output:(When the file uploaded is of type jpg or jpeg & size less than specified size)

Output:(When the file uploaded is not jpg and jpeg type or size more than specified size)

The above code will produce the form elements,
-An element with file type to choose the file.
-A submit button for submitting the form.

When you upload the file then only jpg or jpeg and the size is less than 100000 bytes then files will be uploaded.
If you are trying to upload the file other than jpg or jpeg type and file size greater than 100000(inclusive) then they can’t be uploaded.

To put the restrictions we need to consider following to implement the code successfully,
-First we need to check the extension of the files whether they have jpg or jpeg.
-For checking the extensions, we have string functions.
-Here we are using three string functions for checking the name of the file.
-substr() function to find the substring of the given string.
-strpos() function to find the position of the string or character.
-strtolower() function to convert the given string to lower case.

-we need to declare the maximum size for a file to be uploaded.
-we have file variable to know the size of the file to be uploaded.
-we can get the file size from $_FILES[‘file’][‘size’] .
-So we are placing a condition in if loop to check the file size which is less than the specified size or not.

-Here we are searching the name of the file which ends with jpg or jpeg.
-As every file extension end with ‘.’ here we are searching for it using strpos() function.
-Once we get the string position then we are giving the position to the substr() function by incrementing to 1.This function will take the string after dot’.’, hence we will get the extension.
-The string from substr() function will be given to strtolower() function which will be convert the string to lowercase.
-The string we got by using string functions will be stored in $extension variable.

So we got the extension of the file we are uploading.
-In the above code we are using if condition to check whether the extension of file is jpg or jpeg and the file size is less than 100000.
-If the above condition is satisfied then we are going to upload the file else the file uploading will be failed and a message is displayed.
Hence we have restricted the file upload with type safety and limiting the file size.



Leave a Reply