Upload and Rename a File with PHP
Eventually every programmer will be required to find a way to upload a file to a web server. I recently started a project that required a way for me to upload a word doc to a web server, which, in turn, required me to write a upload and rename script in PHP.
See my updated version of this script called A Better PHP Upload and Rename Script.
So, here are the steps I went through to get a word doc to upload to a directory on my web server.
Step 1: Build the Form
I am just going to assume you know how to build an upload form in HTML. But just in case, here is the code I used:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" name="file_upload"> <input type="file" name="file" id="file"> <input type="hidden" name="execute" id="execute"> <input type="submit" name="Submit" id="Submit" value="Submit"> </form>
This form, of course, lets me post a file to a temp directory on the web server. Once our file is posted, we then validate the file type, size, and if everything checks out, move the file to its final destination.
Step 2: The Code
Here is the code that puts out file on the server, checks the file type, and does some error reporting.
<?php // Upload and Rename File if (isset($_POST['execute'])) { $filename = $_FILES["file"]["name"]; $file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention $file_ext = substr($filename, strripos($filename, '.')); // strip name $filesize = $_FILES["file"]["size"]; if (($file_ext == ".doc" || $file_ext == ".docx") && ($filesize < 200000)) { // rename file $newfilename = md5($file_basename) . $file_ext; if (file_exists("upload/" . $newfilename)) { // file already exists error $error = "You have already submitted this file."; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename); echo "File uploaded successfully."; } } elseif (empty($file_basename)) { // file type error $error = "Please select a file to upload."; } else { // file selection error $error = "Only doc, docx, rtf, pdf, and txt files can be submitted online."; unlink($_FILES["file"]["tmp_name"]); } } ?>
Everything is this code is commented pretty well. The important lines of code here are where the new file name is built ($newfilename) and where the file, if valid, is moved to its final location (move_uploaded_file()). Make sure you set the final file directory in the move_uploaded_file() command. Otherwise, the script will fail.
Added Bonus: This Script Can Upload Anything
Just so you know, you can upload anything to a web server with this script. All you need to do is change the allowed extensions near $file_ext == “.doc”.

Husnain
on May 5, 2009Hi,
It works fine, but this code has a problem, when a file is uploaded twice with same name it does not work. Please inform where to change code.
Thanks
Zac
on May 5, 2009Hi Husnain,
You should have no problems changing the code to work with the same file over and over. Try changing this line:
$newfilename = md5($file_basename) . $file_ext;
to something like
$newfilename = md5(time() . $file_basename) . $file_ext;
Let me know if that works for you.
Saguenay
on May 23, 2009Great tutorial! Thanks!
YooNet
on June 25, 2009Hi,
Nice script, how do i show the url to the file?
Many Thanks!
Zac
on June 25, 2009Hi YooNet,
Try adding this to line 20–it should give you a path to the file:
echo “upload/” . $newfilename;
aleks
on August 8, 2009very well said! thanks for this…it really helps me a lot :)
kind regards,
~aleks
SHW
on September 11, 2009Thanks, man. This was exactly what I was looking for!
You helped me a lot. I just wanted to let you know :)
Greetings from Germany
Asanka lakmal
on October 28, 2009Great work. Thanks very much
Asanka
Juussi
on February 2, 2010Hello. Thanks for this.
Is it possible to ‘post’ also a wanted new file name and rename the sent file in server site?
ajay sarweai
on April 15, 2011Thanks.
brandon
on May 3, 2011tnx
Jannis
on June 15, 2011Nice script! Very useful… ;)
Adnan
on June 30, 2011your code does not work properly
plz check it
Zac
on July 1, 2011Hi Adnan,
I’m fairly certain the script works. I test all code before I publish it. Please give it another shot.
Rafael
on August 2, 2011Code works flawlessly. I made my own modifications but this saved me a ton of time. Thanks Zac!
Junaidu
on September 5, 2011its really works, thanks onceagain.
But one more help. How do i add the new name to the Database?
THANKS IN ADVANCE