Simple File Upload Script in PHP

In this tutorial, you will learn a simple script to upload a file on remote server using a simple HTML form and store in a directory. This can be used to upload any type of file, like - PDF, doc, image, video, zip files and other file type. We can also strict by specifying file type, set the limited file size and much more.

Simple File Upload Script in PHP

To create a file uploader, first create a form and add a file type input field. File type input shows "Browse" button on the webpage. If a form contains any file type input field, then we want to make sure to add a form attribute 'encrypt' with a value 'multipart/form-data'. encrypt specifies the content-type to be uploaded.

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<p>Upload a file: <input type="file" name="attachment" /></p>
<p><input type="submit" /></p>
</form>

PHP has a global array $_FILES, to handle the file upload. This array returns the following data of the uploaded files. Suppose the name of the input field to upload file is 'attachment', then on file upload this array returns -

$_FILES['attachment']['name']
$_FILES['attachment']['type']
$_FILES['attachment']['size']
$_FILES['attachment']['tmp_name']
$_FILES['attachment']['error']

When a file is uploaded, it first saves in a temporary directory on the server. PHP predefined function move_uploaded_file() is used to move the uploaded file from the temporary location to a specified location.

<?php 
// check the file is uploaded or not
if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {    
	// Determine the file location
	$newname = dirname(__FILE__) . '/' .basename($_FILES['attachment']['name']);

	// Move the file from temporary location to specified location
	if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
		echo "<p>ERROR:  A problem occurred during file upload!</p>\n";
	} else {
		echo "<p>The file saved as: {$newname}</p>\n";
	}
}
?>

In the above example, is_uploaded_file() function is used to check the file whether it is successfully uploaded or not. Here, we have merged the above code and get the full script to upload a file.

<html>
  <head>
   <title>Simple File Upload Script in PHP</title>
  </head>
  <body>
  <?php 
  // check the file is uploaded or not
  if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {    
	// Determine the file location
	$newname = dirname(__FILE__) . '/' .basename($_FILES['attachment']['name']);

	// Move the file from temporary location to determined location
	if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
		echo "<p>ERROR:  A problem occurred during file upload!</p>\n";
	} else {
		echo "<p>The file has been saved as: {$newname}</p>\n";
	}
  }
 ?>
 <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"  enctype="multipart/form-data">
  <p>Please Upload a file <input type="file" name="attachment" /></p>
  <p><input type="submit" /></p>
 </form>
 </body>
</html>




Related Articles

Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP





Read more articles


General Knowledge



Learn Popular Language