0

Hello,

My problem stems from reading an image from the local disk and displaying it. There's a middle step of storing it in a database, but I simplified the problem into this test script that replicates the error:

The image "http://example.com/test.php" cannot be displayed, because it contains errors.

test.php looks like this:

$location='/location/to/image.gif';
$size = getimagesize($location);
$img_type = $size['mime'];
$data = file_get_contents($location);
$img_data = addslashes($data);
header("Content-type: ".$img_type);
print $img_data;

A print_r of $size gives me valid information (e.g., $size is "images/gif") so this shouldn't be a permissions problem.

I am using examples from online of uploaded images instead of images grabbed locally.

What might be the issue?

Thanks in advance.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by maxterner
1

Hi,

I removed the "addslashes" line and it seems to work. I used a jpg in my test. You obviously did that to store it in the database, so I guess you need to "removeslashes" before displaying it.

Edited by NettSite: n/a

0

Thanks NettSite for that catch. Little bugs take so long to solve sometimes :)

0

This does not work,
I tried so many time but i get "The image “http://localhost/test.php” cannot be displayed because it contains errors".
check whether this code is correct or not,

$location='C:/xampp/htdocs/dog.jpg';
    $size = getimagesize($location);
    $img_type = $size['mime'];
    $data = file_get_contents($location);
    //$img_data = addslashes($data);
    header("Content-type: ".$img_type);
    print $data;

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.