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.

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.

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

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.