Uploading Image Files with PHP by: bs0d
Page: 5 of 5 (View All)
Conclusion and Debugging
No more code is required to upload a file from this point in the script. Any other coding is up to you for the other parts of your script or website. The first time I began constructing an image upload script, I found it quite useful to display the variables along the way for debugging. Particularly, displaying the array of information about image stored in the $_FILES array. You could use var_dump() to display everything, but its not visually friendly. Instead, here is the same information but broken up a bit to make it easier to read:
Here is all of the code for the script.
if(isset($_POST['submit'])) { //see if submit button is pressed.
//check if they decided to upload a pic:
if($_FILES['userfile']['size'] > 1) {
$max_size = 100000;
$max_height = 300;
$max_width = 300;
$info = getimagesize($_FILES['userfile']['tmp_name']);
//check file-size (in bytes):
if(($_FILES['userfile']['size'] > $_POST['MAX_FILE_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) {
die(" Error: Upload file size too large: (" . $_FILES['userfile']['size'] . " ). Must not exceed XX kb.");
}
//check the extension.
$array = explode(".", $_FILES['userfile']['name']);
$nr = count($array);
$ext = $array[$nr-1];
if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png"))
die(" Error: file extension un-recognized. Be sure your image follows the correct extension (.JPG or .PNG)");
//CHECK TYPE: (what the browser sent)
if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) {
die(" Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
}
//DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE!
if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) {
die(" Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
}
//check file size (length & width)
if(($info[0] > $max_width) || ($info[1] >$max_height)) {
die(" Error: Image size error (" . $info[0] . " x " . $info[1] . " ). Must not exceed ". $max_height . " x ". $max_width .".");
}
//rename file, move it to location.
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
//get max number of images the user has uploaded
$m = mysql_query("SELECT max(user_images) as `total_images` FROM `images` WHERE `user_id` = '".$_SESSION['user_id']."'");
if(!$m) die('An Error Occurred.');
$result = mysql_fetch_object($m);
if($result->total_images <= 0) {
$image_number = 1;
} else {
$image_number = $result->total_images + 1;
} //end if
$filename = strtolower($_SESSION['username']) . $image_number;
if(move_uploaded_file($_FILES['userfile']['tmp_name'] , $_SERVER['DOCUMENT_ROOT']."/path/to/image/".$filename . '.' . $ext)) {
echo("File uploaded successfully.");
} else {
echo("An error occurred while uploading.");
}//end upload
} //end is_uploaded_file
} else { //display form ?>
} //end else ?>
Displaying the value of other variables as you progress through your script will also aid in any complications you encounter. As always, feel free to post any questions about the script or any other subject for that manner in the Forums for help. Thanks for reading, I hope you found value in the article and it will help you get set in the right direction.
-bs0d | AllSyntax.com
Comments:
David1159
Subject: "Fixed Error, $end"
Date: Dec 29 2007 at 3:06 pm
Part -- Accessing the File
Current Code-
Code: $info = getimagesize($_FILES['userfile']['tmp_name']);
Should Be-
Code: $info = getimagesize($_FILES['userfile']['tmp_name']);
}
Cheers,
David
barbs75
Subject: "multiple image uploads?"
Date: Apr 23 2008 at 8:31 am
Hey,
VERY GOOD TUTORIAL!!! nice and easy to follow!
one question i do have though, is how would you upgrade this to deal with multiple image uploads in one form?
That is what i am trying to do for my website at the moment, do you simply put the extra image upload fields within the form?
but how would you validate these images?
cheers
Craig
Tutorial Stats
30,219
Views
7
Total Comments
5
Rating of 5 (1 Votes)