PHP File Upload Tutorial – Learn How to Upload Files Using PHP – Download the Script FREE!

File Upload Script

Download the Files Here!

Download Link

 

Learn the Code

 

Learning how to use PHP to Upload Files to your Server is one of the most useful skills you can accrue and this tutorial will show you how to do it!

We are going to develop this script from a blank canvas, so create a new PHP document and select an area that you would like to insert your Upload Form into.

When you are ready, place your cursor wherever you would like your upload form to appear and switch into code to paste the code below:

 

<form action="FileUpload.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
    <label for="UploadFileField"></label>
    <input type="file" name="UploadFileField" id="UploadFileField" />
    <input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
  </form>

 

At this point all we have is a form. We need to create some PHP that will handle the files and uplaod them to your sever.

Here is the script that will process the files and upload them to your server. Make sure you paste this code into line 1 of the same document that you inserted your form into.

 

<?php
	if(isset($_FILES['UploadFileField'])){
	    
		// Creates the Variables needed to upload the file
		
		$UploadName = $_FILES['UploadFileField']['name'];
		$UploadName = mt_rand(100000, 999999).$UploadName;
		$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
		$UploadType = $_FILES['UploadFileField']['type'];
		$FileSize = $_FILES['UploadFileField']['size'];
		
		// Removes Unwanted Spaces and characters from the files names of the files being uploaded
		
		$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
		
		// Upload File Size Limit 
		
		if(($FileSize > 125000)){
			
			die("Error - File to Big");
			
		}
		
		// Checks a File has been Selected and Uploads them into a Directory on your Server
		
		if(!$UploadTmp){
			die("No File Selected, Please Upload Again");
		}else{
			move_uploaded_file($UploadTmp, "Upload/$UploadName");
		}
	}
?>

 

Please reference to Comments within the Code to understand how to modify the script further to your requirements

Where are the Files Saved?

The script is programmed to upload the files to a folder called ‘Upload’ that is within the same directory as the script.

Makes sure to create this folder or modify the script to successfully upload the files!

 

 

One thought on “PHP File Upload Tutorial – Learn How to Upload Files Using PHP – Download the Script FREE!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>