I’m learning PHP & I’m working on a simple upload and display image exercise out of a Apress PHP book. I’m using the latest Eclipse program, XAMPP, Apache, etc…
The problem I’m having is that the image being uploaded is a JPEG but it is being read as type PJPEG. It pushes my IF
statement to the FALSE
section with my error messages.
If I change the all the criteria to pjpeg
, then for some reason I get weird raw code instead of a picture.
How do I ensure that my image being loaded stays a jpeg or how can I rewrite my code to upload and output a pjpeg
and actually display in browser.
UPDATE: the code provided by Jake is working but the image itself is not displaying in browsers. Tested in Chrome, Firefox and IE.
<?php
error_reporting(E_ALL);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ( isset($_FILES['photo']) &&
is_uploaded_file($_FILES['photo']['tmp_name']) &&
$_FILES['photo']['error']==UPLOAD_ERR_OK ){
if ($_FILES['photo']['type'] == 'image/jpeg'){
$j = imagecreatefromjpeg($_FILES['photo']['tmp_name']);
header('Content-Type: image/jpeg');
imagejpeg($j);
imagedestroy($j);
} else {
echo "Uploaded file was not a jpeg. <br />";
echo "The file ", $_FILES['photo']['name'], " uploaded was a ", $_FILES['photo']['type'];
}
} else {
echo "No photo upload";
}
} else {
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" name="submit" value = "Upload a JPEG" />
</form>
<?php } ?>