A simple image upload script

December 4th, 2007 by admin

Have you ever wanted to make an image upload and display script, but thought it would be hard? Well, actually uploading files with a form is actually really easy. In this tutorial I will show you how to upload an image, display file information about the image and if the image is a jpeg display it. This is a fun piece of code for beginners to play with and could be the basis of a larger image gallery script.


What you will need to do is first create a directory for the script, lets call it imageupload. Then make another folder called ‘upload’ inside the imageupload folder this is where we will store the uploaded images. Then we will create a file called imageupload.php with the following code inside it:

imageupload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<html>
<form enctype="multipart/form-data"
	action="<?print $_SERVER['PHP_SELF']?>" method ="post">
 
<tr><td><input type = "hidden" name="MAX_FILE_SIZE" value = "102400"></td></tr>
<tr><td>Select File: </td><td><input type = "file" name = "fupload"><t/d></tr>
<tr><td><input type = "submit" value = "Upload!"></td></tr>
</table>
<?php
//checking and uploading file-----------------------------------------------------------
if (isset ($_FILES['fupload'])){
 
$filename = $_FILES['fupload']['name'];
$randomdigit = rand(0000,9999);//create random digit
$newfilename = $randomdigit.$filename;//make new file name with random digit
 
//printing file information
print "<table>";
print  "<tr><td>Original Name:</td><td> ". $_FILES['fupload']['name']."</td></tr>";
print	"<tr><td>New Name:</td><td> ".$newfilename."</td></tr>";
print  "<tr><td>Size: </td><td>". $_FILES['fupload']['size']."</td></tr>";
print  "<tr><td>Temp Name: </td><td>". $_FILES['fupload']['tmp_name']."</td></tr>";
print  "<tr><td>Type: </td><td>". $_FILES['fupload']['type']. "</td></tr>";
print  "<tr><td>Error: </td><td>". $_FILES['fupload']['error']. "</td></tr>";
print "</table>";
 
 
//checking the type of file, if it is image it will display it
if ($_FILES['fupload']['type'] == "image/jpeg"){
	$source = $_FILES['fupload']['tmp_name'];
	$target = "upload/".$_SESSION['username']."/".$newfilename;
	move_uploaded_file($source, $target); // or die ("Couldnt copy");
 
 
	//displaying the image
	$imagesize = getImageSize($target);
	$imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" ";
	$imgstr .= "src=\"$target\" alt=\"uploaded image\" ></p>";
 
	$imagepath = $yoursite.$target;
 
	print $imgstr;
	print "The link to your image is: ".$yoursite.$target;//link to the image
 
 
}
}
//-------------------------------------------------------------------------------------
?>
</html>

A quick explanation on what we have done:
On lines 2-8 we create the file upload form and on line 5 we have a hidden form field that contains the maximum file size for the file to be uploaded.
On lines 14 and 15 we create a random digit and append it to the original file name, this way we can deal with any duplicates.
On lines 18-25 we display information about the file.
And on the last section of code we move the file to the upload directory and display it on the page.

Hope you enjoy this code!

del.icio.us Digg Technorati StumbleUpon

Posted in General Opensource

9 Comments

  1. Robin

    Very nice man. Keep the posts coming!

  2. Goorgoor

    Hi,
    How can i allow ppl to upload other files! like zip? like gif?

  3. jason

    Hi Goorgoor,
    you can infact upload .gif with this script. It just wont display because of the check if you changed the check to $_FILES['fupload']['type'] == “image/gif” it would display only gifs. For multiple file types, just put an OR statement between the checks. As for uploading zip files and extracting, this script was not meant for that use. Thanks for the comment.

  4. Shogun

    Yo!
    This is most easy image upload script that i have see.

    Thx!!

  5. PHP: Upload and resize image script | TheOpenSurgery

    [...] images. If you have not read my tutorial on image uploading I would suggest you also check it out here. I have also supplied my code for download and use at the end of this [...]

  6. Juan Carlos Rois

    Your script works perfectly! Thanks for posting it and keeping it simple.
    “Less is more”

  7. Peter Armenti

    Hi.. where in this script is it saying what directory and folder this is uploading to? For some reason I don’t see that. Thank you.

  8. veselin

    Hello!
    Thanks man!
    This script saved me hours to write it and think it! You have one beer from me :D
    Thanks again!
    Have a nice day!

  9. Jason

    I am trying to get your upload script working, what do I need to edit for this to function? I thank you so much for this, I am a beginner, and really need this script. I created the directory and folder, but the images I tried to test never showed up. Thanks a million!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.