thefall Posted April 13, 2014 Share Posted April 13, 2014 Hi. I have a problem where when i try to display the uploaded JPEG file, the browser would only dispay the placeholder for an image . I think the problem is within the 3rd IF statement. Please help me thanks. <?php 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') { echo 'asdsad'; $tmp_img = $_FILES['photo']['tmp_name']; $image = imagecreatefromjpeg($tmp_img); header('Content-Type: image/jpeg'); imagejpeg($image, NULL, 90); imagedestroy($image); } else { echo "Uploaded file was not a JPG image."; } } else { echo "No photo uploaded!"; }} else { echo " <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> ";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/ Share on other sites More sharing options...
Ch0cu3r Posted April 13, 2014 Share Posted April 13, 2014 Comment out this line (add // infront of it) header('Content-Type: image/jpeg'); Run your code again. Are you getting any errors? Note when using header() no output (echo's, whitespace, text etc) can be sent before using it. Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475984 Share on other sites More sharing options...
thefall Posted April 13, 2014 Author Share Posted April 13, 2014 @Ch0cu3r I tried removing the header earlier and it would show some weird characters. I've read somewhere that the header is necessary to remove the weird characters. I ran the code on firefox and the message "The image http://localhost/test/test.php cannot be displayed because of errors" appeared. Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475987 Share on other sites More sharing options...
Ch0cu3r Posted April 13, 2014 Share Posted April 13, 2014 tried removing the header earlier and it would show some weird characters The characters is the binary of the image. I was aware this was going to happen. If you look closely before the weird characters is there any error messages (to make it clearer you can also comment out imagejpeg() too)? Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475989 Share on other sites More sharing options...
thefall Posted April 13, 2014 Author Share Posted April 13, 2014 (edited) @Ch0cu3r I converted the imagejpeg() and header lines into comments. There were no error messages on google chrome and firefox. What I've noticed is that the line below would be displayed if you remove the imagejpeg() and header lines, whereas if you include the imagejpeg() and header lines, the line below wouldn't be displayed .I inserted the line below in the code, just to check where it would stop being displayed. echo 'asdsad'; Edited April 13, 2014 by thefall Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475993 Share on other sites More sharing options...
Ch0cu3r Posted April 13, 2014 Share Posted April 13, 2014 What I've noticed is that the line below would be displayed if you remove the imagejpeg() and header lines, whereas if you include the imagejpeg() and header lines, the line below wouldn't be displayed Doing that will corrupt the image. I only got you to comment out those lines so you can check for errors. Does uncomentting the header/imagejpeg lines and removing the echo before those display the image now? Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475997 Share on other sites More sharing options...
thefall Posted April 13, 2014 Author Share Posted April 13, 2014 @Ch0cu3r I removed the header, imagejpeg and echo lines. Chrome and firefox would just display a blank page. I thought imagejpeg() was necessary to display a jpeg image? Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475998 Share on other sites More sharing options...
Ch0cu3r Posted April 13, 2014 Share Posted April 13, 2014 I did not tell you to remove the header/imagejpeg only the echo. Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1475999 Share on other sites More sharing options...
thefall Posted April 13, 2014 Author Share Posted April 13, 2014 @Ch0cu3r Sorry, I've misread your post. I removed the echo and the problem is still the same. Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1476000 Share on other sites More sharing options...
jazzman1 Posted April 13, 2014 Share Posted April 13, 2014 Your script from 1st post works for me, comment out this line - echo 'asdsad'; Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1476008 Share on other sites More sharing options...
Solution thefall Posted April 14, 2014 Author Solution Share Posted April 14, 2014 @jazzman1 and @Ch0cu3r thanks it's working now. Quote Link to comment https://forums.phpfreaks.com/topic/287742-display-jpeg-image-problem/#findComment-1476064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.