Twitter
RSS

A Better PHP Upload and Rename Script

March 15, 2010 - 3 Comments Bookmark and Share

upload_arrowAn earlier post of mine provides a fairly good example of a PHP script that renames a file as you upload it to a server. In this post, I’ve decided to take another look at that type of script and make it easier to use. I’m going to break the code you’ll need into two parts: the HTML and the PHP. Once you have this script on your server, make sure you create a writable folder (I call mine “upload”) on your server to contain your uploaded files.

HTML

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

PHP

<?php
 
// Upload and Rename File
 
if (isset($_POST['submit'])) {
 
	$filename = $_FILES["file"]["name"];
	$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
	$file_ext = substr($filename, strripos($filename, '.')); // get file name
	$filesize = $_FILES["file"]["size"];
	$allowed_file_types = array('.doc','.docx','.rtf','.pdf');
 
	if (in_array($file_ext,$allowed_file_types)  &&  ($filesize < 200000)) {
 
		// rename file
		$newfilename = md5($file_basename) . $file_ext;
 
		if (file_exists("upload/" . $newfilename)) {		
			// file already exists error
			echo "You have already uloaded this file.";			
		} else {		
			move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
			echo "File uploaded successfully.";			
		}
 
	} elseif (empty($file_basename)) {	
		// file selection error
		echo "Please select a file to upload.";		
	} elseif ($filesize > 200000) {	
		// file size error
		echo "The file you are trying to upload is too large.";		
	} else {	
		// file type error
		echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
		unlink($_FILES["file"]["tmp_name"]);		
	}
 
}
 
?>

Having a script like this in your arsenal is a great tool. Developers often need to have a user upload a file back the server, making this script a nice trick in the bag. And if you look closely, it isn’t very complicated. In lines seven through eleven of the PHP, we define some variables that get used in the rest of the script. Pay particular attention to the array called $allowed_file_types. That array determines the types of files a user can upload with this script. You’ll also want to pay attention to line sixteen on the script, which is where we rename the file. I am just md5-ing the file name, but you can use anything you want here.

Comments

  1. Khim Sambo Rothana(June 21, 2010)

    I’m so happy to see your brilliant code. 100% perfectly works. Thank you so much for your contribution.

  2. Khim Sambo Rothana(August 31, 2010)

    Hey, guys!

    I have to change the file size to be less then 4MB (<4194304), but it's not working. Why? I can still upload the bigger size.

    Please help!

    Good luck,
    Rothana

  3. Zac(September 3, 2010)

    I’m not too sure why it isn’t working. Most PHP servers, by default, only put caps on how big a file can be. In the code, this is where I’m putting limits on the file size: $filesize < 200000.

Leave a Reply