Simple File Uploading with PHP
February 22, 2008
In this tutorial I am going to show you how to create a simple upload script which will enable you to upload .gif, .jpg and .png images. This can be enhanced to allow more formats if you wish but I will not cover this in this tutorial.
Firstly we want to create the file, I am going to call it upload.php.
Creating the form
Add the following code:
<html> <body><form method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file" /> <br/> <input type="submit" name="submit" value="Upload" /> </form></body> </html>
This is the html code for the form, it adds a simple file upload field and an upload button. The upload field will have a browse button, the user will click this to locate the file on their computer and then click the Upload button to upload it to the server.
Creating the PHP code
We will now look at creating the backend php code that will add the functionability, to do this copy and paste this code below the html code you added earlier:
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2097152)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else {if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Uploaded"; } } } else { echo "Invalid file"; } ?>
OK, a quick explanation of the code above.
- Checks what the file format of the file selected is
- If it is a format that is allowed (gif, jpeg, png, x-png and pjpeg) then it will check the file size. I have set the maximum file size to around 2MB, this is measured in bytes (2097152)
- If there was an error then it will show the error message (echo “Return Code: ” . $_FILES["file"]["error"] . “<br />”;)
- Then it checks whether the file exists in the “uploads” folder (the folder that I have specified to store the files), if it exists then an error message is displayed, if not then it uploads the file to the “uploads” folder and returns the word “Uploaded” to the screen.
There are a lot of checks to do because there can be a lot of things that go wrong in file uploading.
A few things to do to make it all work properly…
- You need to make sure that your uploads folder is granted access rights by users of the site, in order to do this ensure that you chmod the folder to 777. There should be an option to change the permissions of a folder on your file manager of the web host area.
- Sometimes you may get an error saying that there is a problem with the memory handling, this is because you haven’t set an appropriate memory limit. To set it add this code just under the <?php :
ini_set(”memory_limit”,”12M”);
Conclusion
I think that is it, you should now have a working upload script, you can change it to add extra feature such as connecting it to a MySQL database and storing data about the image. There are plenty of tutorials around the internet with more complex upload scripts so take a look around, this is just a starting point but should be sufficient for simple uploading.





James Little
March 22nd, 2008 at 7:05 am
Thanks, I have never worked with files before in php, this helped me out a lot. I am going to use this in my avatar system.
-James