Simple PHP upload file script with size and file type limitation

Simple PHP upload file script with size and file type limitation, this is a simple script for uploading files into web-server using PHP. Here I’ll show you, restricting file size and also restricting file types. Here I’ll show you how to upload multiple files using PHP. Here you can find step by step code and it is simple to understand the code. If you want you can download complete script, you can find download link bottom of this article.

1

Creating Simple PHP upload file script

Before continuing with PHP code, add a simple form to your HTML code inside body section with enctype “multipart/form-data“. First I’ll create Simple PHP file upload script, then I’ll extend that script with Size limitation and file type limitation.

<form action="" method="post" enctype="multipart/form-data">
	<input type="file" name="file"/> <br /><br />
	<input type="submit" value="submit" />
</form>

While uploading file, all the file data will be stored in an $_FILES super global as an array. Files super global contains data of name of the file, file type, temporary location of file, size of the file, and error code also.

You can check Files super global by using this code. Add this code before starting HTML section, and submit file.

<?php
echo "<pre>";
print_r($_FILES);
echo "</pre>";
?>

After processing this code, you will know what data Files super global contains. Below is an sample array of data.

PHP upload file, files super global data

PHP upload file, files super global data

a. Assigning super globals into variables

Next step in file upload script is I’ll assign these Files super global data into some variables. Remove previous code and add these below lines of code before starting html section.

<?php

$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];

$tmp_name = $_FILES['file']['tmp_name'];

?>

Here in above step I’m assigning all files super global data into variables.

b. Checking file is empty or not?

Next step is to check file is empty or not? If it’s not empty then we will upload it into directory on web-server. Add this code after the above code.

if(isset($name)){
	if(!empty($name)){
		// file upload code goes here.
	}else{
		echo "Please select a file";
	}
}

c. Code for file uploading

If it passes file checking, then we need to execute PHP file upload code, add this below in above commented code section. By default uploaded file be placed in temporary location, here by using move_uploaded_file function, I’m moving this file from temporary location to uploads directory with original file name.

$location = 'uploads/';
		if(move_uploaded_file($tmp_name, $location.$name)){
			echo "uploaded";
		}else{
			echo "Error in uploading file";
		}

Final complete code

After Combining all these code snippets, final code will look like this.

<?php
$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];

$tmp_name = $_FILES['file']['tmp_name'];

if(isset($name)){
	if(!empty($name)){
		$location = 'uploads/';
		if(move_uploaded_file($tmp_name, $location.$name)){
			echo "uploaded";
		}else{
			echo "Error in uploading file";
		}
	}else{
		echo "Please select a file";
	}
}
?>
2

php upload file size limit

In a step, define what is maximum file size in bytes. You can add this code, here I’m defining 2MB maximum file size.

$max_size = 2097152;

Add this code in b step comment area //file upload code goes here, add this below code after that comment.

if($size<=$max_size){
			$location = 'uploads/';
			if(move_uploaded_file($tmp_name, $location.$name)){
				echo "uploaded";
			}else{
				echo "Error in uploading file";
			}
		}else{
			echo "file size is more than maximum";
		}
3

php upload file type extension limiting

Here I want to upload an image file, I’m allowing only jpg image file type. You can add many more file types. First of all I’m filtering uploaded file type, using below code.

$ext = substr($name, strpos($name, '.') + 1);

After maximum file size condition add an if condition to check file type of uploaded file. If it passes file types then it will proceeded to next step.

if(isset($name)){
	if(!empty($name)){
		if($size<=$max_size){
			if(($ext == 'jpg' || $ext == 'jpeg')&&$type=='image/jpeg'){
				$location = 'uploads/';
				if(move_uploaded_file($tmp_name, $location.$name)){
					echo "uploaded";
				}else{
					echo "Error in uploading file";
				}
			}else{
				echo "file should be jpg/jpeg";
			}	
		}else{
			echo "file size is more than maximum";
		}
	}else{
		echo "Please select a file";
	}
}

Final code for php upload file script

<?php
$name = $_FILES['file']['name'];

$size = $_FILES['file']['size'];

$type = $_FILES['file']['type'];

$tmp_name = $_FILES['file']['tmp_name'];
$max_size = 1000000;

$ext = substr($name, strpos($name, '.') + 1);
if(isset($name)){
	if(!empty($name)){
		if($size<=$max_size){
			if(($ext == 'jpg' || $ext == 'jpeg')&&$type=='image/jpeg'){
				$location = 'uploads/';
				if(move_uploaded_file($tmp_name, $location.$name)){
					echo "uploaded";
				}else{
					echo "Error in uploading file";
				}
			}else{
				echo "file should be jpg/jpeg";
			}	
		}else{
			echo "file size is more than maximum";
		}
	}else{
		echo "Please select a file";
	}
}
?>
<form action="" method="post" enctype="multipart/form-data">
	<input type="file" name="file"/> <br /><br />
	<input type="submit" value="submit" />
</form>

Note: If you are facing any problem while uploading please check that a directory with name of uploads is present or not. If it’s not create it. This code is only for learning process, don’t use this on production server.

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: