Simple PHP Image Upload Form

This little chunk of PHP allows a user to upload a JPEG image and then resizes that image to generate a thumbnail automatically. With a little work, you can change the file types allowed, formula for resizing the thumbnails, etc. but I wanted to keep this simple so only JPEGs are allowed and everything is resized based on height.


$allowedfiletypes = array("jpeg","jpg");
$uploadfolder = "uploads/" ;
$thumbnailheight = 100; //in pixels
$thumbnailfolder = $uploadfolder."thumbs/" ;

$action = $_POST['action'];
if ($action == "upload") {
    echo "<p>Uploading image... " ;
        echo "<strong>Error: File not uploaded!</strong></p>\n\n" ;
    } else {
        $uploadfilename = $_FILES['uploadimage']['name'];
        $fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,".")+1));
        if (!in_array($fileext,$allowedfiletypes)) { echo "<strong>Error: Invalid file extension!</strong></p>\n\n" ; }
        else {
            $fulluploadfilename = $uploadfolder.$uploadfilename ;
            if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) {
                echo "$uploadfilename has been uploaded succesfully.</p>\n\n";
                $im = imagecreatefromjpeg($fulluploadfilename);
                if (!$im) { echo "<p><strong>Error: Couldn't open image to create thumbnail!</strong></p>\n\n" ; }
                else {
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);
                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $thumbfilename = $thumbnailfolder.$uploadfilename ;
                    imagejpeg($newim, $thumbfilename) or die("<p><strong>Error: Couldn't save thumnbail!</strong></p>");
                    echo "<p><img src=\"$thumbfilename\"></p>\n\n" ;
            } else { echo "<strong>Error: Couldn't save file ($fulluploadfilename)!</strong></p>\n\n"; }

echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" enctype=\"multipart/form-data\">\n" ;
echo "<input type=\"hidden\" name=\"action\" value=\"upload\" />\n" ;
echo "JPEG Image File: <br />\n" ;
echo "<input type=\"file\" name=\"uploadimage\" /><br /><br />\n" ;
echo "<input type=\"submit\" value=\"Upload Image\" />\n" ;
echo "</form>\n\n";


After you've uploaded it, here's how you can easily resize your image on the fly.


Viet Tran
April 12, 2011, 6:28 am
Thanks for useful code
April 16, 2011, 8:20 pm
awesome, thx
April 18, 2011, 6:13 am
Thnkx for nice php upload script.
July 10, 2011, 11:06 pm
I keep getting Warning: move_uploaded_file(uploads/DSCN0371.JPG) [function.move-uploaded-file]: failed to open stream:
July 11, 2011, 9:06 am
There's a couple different things that might be causing that. Make sure that the uploads directory you're using exists and that it is writable to PHP. (PHP's chmod function can help you with the permissions.) Hope that helps.
July 11, 2011, 5:05 pm
Tks I changed the permission and now for some reason I get 500 Internal Server Error
July 13, 2011, 9:52 am
You can try turning on PHP's error reporting to see what exactly the error being caused is.
October 26, 2011, 11:02 am
Just what i was looking for thanks alot! :)
November 7, 2011, 6:38 pm
Excellent! Thank you for sharing.
October 12, 2012, 5:36 am
Why it getting warning : Uploading image… Error: Couldn't save file (uploads/Autumn Leaves.jpg)!
October 15, 2012, 10:22 am
Why it getting warning : Uploading image… Error:
Robert James Reese
October 15, 2012, 8:09 pm
If you're getting an error saving, you should check that your script has permissions to write in the folder that you're trying to upload to.
January 21, 2013, 10:59 pm
Very helpful! Thanks
February 9, 2013, 1:09 am
nice post and very informative code it solved my problem
February 19, 2013, 3:39 pm
Thanks buddy, may skies help you in the future.

Leave a Comment

Name and email are required. Your email will not be published.

This post was published on January 17th, 2009 by Robert James Reese in the following categories: HTML, Images, and PHP. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.