Skip to main content

Simple File Upload with PHP

In this post I am going to show you how to upload file to server using simple PHP and HTML code. Uploading files with php is one of the most fundamental programming skill. This script will allow you to upload single image file and store it to the specified directory to the server for later usage.

Source:

<?php
valid_formats = array("jpg","jpeg","JPG", "png", "gif", "bmp");
$max_file_size = 1024*1024*1024;
$dir = "uploads/";

// If File Submitted
if($_SERVER['REQUEST_METHOD'] == "POST"){
	$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
	if ( in_array($ext, $valid_formats)) {
		if( $_FILES['file']['size'] < $max_file_size ){
			$uniq = base_convert(uniqid(), 16, 10);
			$tmp = $_FILES['file']['tmp_name'];
			$uniq_file_name = $uniq.".".$ext;
			if(move_uploaded_file($tmp, $dir.$uniq_file_name)){
				$query = mysql_query("INSERT INTO files (id, file) VALUES (null, '{$uniq_file_name}')");
				$msg = "Uploading successful!";
			}
			else{
				$msg = "Problem while moving file";
			}
		}
		else{
			$msg = "File is too large";
		}
	}
	else{
		$msg = "Worng file format";
	}
}
?>


<html>
<head>
	<title>Simple PHP File Uploader</title>
</head>
<body>
<?php
//Show message
if(isset($msg)){
	echo "<p>{$msg}</p>\n";
}
?>
<form action="" method="POST" enctype="multipart/form-data">
	<label for="file">File:</label>
	<input type="file" name="file" id="file" /> 
	<br />
	<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

If we look over the script we can see that there is two separate part in this script. First one is the PHP and the second one is HTML form. HTML form will allow you to upload file using web browser and the PHP code will catch your uploaded file into the server. It will look up the  validation of your upload, give it a unique name and store it to your defined directory,

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.