File uploading concept in PHP

File uploading concept in PHP
File uploading concept in PHP

Topic – 51
File uploading concept in PHP
Part – 3



-In last topic we have seen how to upload a file into server.
-We have uploaded the files in to the server.
-We have uploaded all types of files into the server.

You can also put some restrictions for uploading the files into server.
-You can restrict the file uploading by size.
-You can restrict the file uploading by type.

Here we are going to discuss about the type safety and file size restriction of the file.
-In this topic we are going to upload the files of images only and if the files other than images are selected then they should not upload into the server.
-If you want to upload the files of type jpg and jpeg then only those types of files should be uploaded.

Let us consider the following program,
$name = $_FILES[‘file’][‘name’];
$type = $_FILES[‘file’][‘type’];
$tmp_name = $_FILES[‘file’][‘tmp_name’];
$extension = strtolower(substr($name,strpos($name,’.’)+ 1));
if( isset($name) ){
if($extension ==’jpg’|| $extension ==’jpeg’){
$location = “uploads/”;
echo “File Uploaded Sucessfully..!!”;
echo “File Uploading failed..!!!”;
echo “File must be jpg or jpeg”;
else {
echo “Please Select a File”;
echo “There was an error in uploading 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: (If the file is of type jpg or jpeg)
File Uploaded Sucessfully..!!

Output :( if the file is not a jpg or jpeg file)
File must be jpg or jpeg

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 files will be uploaded.
If you are trying to upload the file other than jpg or jpeg type 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.

-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 is jpg or jpeg.
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 for file upload with type safety to upload only the images.



Leave a Reply