File uploading concept in PHP

File uploading concept in PHP
File uploading concept in PHP

File uploading concept in PHP
Topic – 51
Part – 4



-In last topic we have seen how to upload a file into server with type safety.
-We have uploaded the files in to the server with type safety.
-You can restrict the file uploading by size.

Here we are going to discuss about restriction of the file size.
-In this topic we are going to restrict the file size.
-If you want to upload the file more than the size specified then it.

-When you are installing PHP in your system, by default the maximum size for uploading a file is set to the limit of 2 MB.
-The upload limit is set by upload_max_filesize=2M in php.ini file.
-You can configure the file size limit in php.ini.
-But if you want to set the maximum file size then you can declare that in your code but which has to be less than upload_max_filesize in php.ini file.

-Let us consider the following program,
$name = $_FILES[‘file’][‘name’];
$size = $_FILES[‘file’][‘size’];
$tmp_name = $_FILES[‘file’][‘tmp_name’];
if( isset($name) && !empty($name)){
if($size < $max_size){
$location = “uploads/”;
echo “File Uploaded Sucessfully..!!”;
echo “File Uploading failed..!!!”;
echo “<br>File size must be less than $max_size”;
echo “<br> The size of the file $name you are trying to upload is $size”;
else {
echo “Please Select a File”;
<form action=”upload_size.php” method=”post” enctype=”multipart/form-data” >
<br><br><input type=”file” name=”file”/><br><br>
<input type=”submit” value=”upload” />

Output:(When uploading a file of size less than specified size)

Output:(When uploading a file of 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 a file it must be less than the specified size. If size of the file is exceeded than specified size then that file should not be uploaded.
To put the restrictions we need to consider following to implement the code successfully,
-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.
Hence we have restricted file upload with size limit.



Leave a Reply