Hi
I'm trying to learn PHP with this book: PHP for absolute beginners by Jason Lengstorf i got stuck with this code:
Code:
<?php
// Checks if the form was submitted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Checks if a file was uploaded without errors
if(isset($_FILES['photo'])
&& is_uploaded_file($_FILES['photo']['tmp_name'])
&& $_FILES['photo']['error']==UPLOAD_ERR_OK) {
// Checks if the file is a JPG image
if($_FILES['photo']['type']=='image/jpeg') {
$tmp_img = $_FILES['photo']['tmp_name'];
// Creates an image resource
$image = imagecreatefromjpeg($tmp_img);
// Tells the browser what type of file
header('Content-Type: image/jpeg');
// Outputs the file to the browser
imagejpeg($image, '', 90);
// Frees the memory used for the file
imagedestroy($image);
} else {
echo "Uploaded file was not a JPG image.";
}
} else {
echo "No photo uploaded!";
}
} else {
// If the form was not submitted, displays the form HTML
?>
<form action="test.php" method="post"
enctype="multipart/form-data">
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" value="Upload a Photo" />
</form>
<?php } // End else statement ?>
when i try it in the browser i only get an error message:
"the image: "http://localhost/marco/phpscripts/test.php" cannot be displayed because it contains errors"
The most common issue for this error is the JPG is saved with CMYK encoding not RGB encoding. This is more often associated with older versions of Firefox. Check to see if the images are saved as RGB and then see if the work in IE. If it works consider upgrading Firefox.
The code worked perfectly for me.
Thank you for your answer, i still can't get it to work though. I am running the latest Firefox, chrome and safari, i'm using the latest version of internet explorer is also .
Could it be the settings for the localhost (i am running xampp 1.81)?
Because I uploaded to a remote server and it seems to work. But i cannot see where the image gets stored.
Thanks
If you are going to output the image, then you cannot output anything except the image, so maybe:
Code:
// Creates an image resource
$image = imagecreatefromjpeg($tmp_img);
// Tells the browser what type of file
header('Content-Type: image/jpeg');
// Outputs the file to the browser
imagejpeg($image, '', 90);
// Frees the memory used for the file
imagedestroy($image);
exit;
}
You also need to make sure nothing else is output before the image output, such as a BOM at the start of your script if it's saved as UTF-8, or anything else that is not part of the image data.
Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett
"But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html
Bookmarks