0

I have found a lot of posts that describe the same problem, but everything I tried failed. For a profile page I am making people can upload 4 pictures. When no picture is available I want to show a default picture. My complete code is like this now:

$id = $_GET['ID'];
$link = mysql_connect("xxx", "xxx", "xxx");
mysql_select_db("xxx");
$sql = "SELECT * FROM profielen WHERE ID = $id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

header("Content-type: image/jpeg");

if ($row['Foto4'] == NULL){
    //echo '<img src="/img/noImage.jpg" />';
    echo $row['Foto5'];
}
else {
    echo $row['Foto4'];
}

In this form the code works, so I know that the if-statement is correct. When I try to uncomment the commented line, it shows a broken link as the image.

I have tried with double quotes and then escape the double quotes within the img tag. I have also tried to call it as a variable and without a / before the img-path. And also just tried to say echo "No image", but it seems that if I do anything else than echo $row[x] it just does not work. When I try to display the image in HTML it works fine, so the name of the file is correct.

I am running out of ideas, so maybe someone can help?

3
  • 4
    The mysql extension is depricated... you do not do any error checking or handling at all... your code allows sql injection... Commented Nov 11, 2013 at 21:38
  • What do Foto5 and Foto4 contain? Commented Nov 11, 2013 at 21:39
  • 1
    You should be outputting binary, not HTML. Commented Nov 11, 2013 at 21:40

2 Answers 2

1

The problem with your code is that you're sending HTML to embed an image back, when really, you should be sending image data back. You can solve this by redirecting the user to the "noImage.jpg", in this way the user will get an image back from the request and all will be fine :)

Code for this could look like this:

if ($row['Foto4'] == NULL){
    header('Location: /image/noImage.jpg');
} else {
    echo $row['Foto4'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Waw, this was stupid of me, I already used this on another page, so I should have known this. Thanks a lot! I was so focused on the image-thing that I did not see this. You saved me a lot more hours of searching :-)
0

If you use the content type image/jpeg, the browser expects the raw binary data of an image. Instead, you give it HTML-code.

You can instead use the Location-header, which tells the browser to open another address.

header("Location: /img/noImage.jpg");

Remember that you cannot output any other data or write any other headers if you use this method.

2 Comments

I accidentally wrote 'Redirect:' instead of 'Location:', fixed that horrible error.
Ah, that's too bad. Would have been much more helpful if the mystery downvoter had commented or proposed an edit. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.