I run PHP 5.2 on Ubuntu with Apache 2.
When I want to serve a PNG file from PHP file which is hardcoded (using $imgname), I can succeed. However if I change the $imgname to $imgdbname, the browser fails and says it is not a valid PNG file.
$imgname = "cachepics/c3s9taopkp.png";
$imgdbname = readfromdb();
header("Content-type: image/png");
print file_get_contents($imgname);
However if I comment the header("Content-type: image/png"); line, browser shows the all PNG garbage text in both cases. Therefore, I am sure the PHP instance is able to reach that file by reading the path from DB.
The ouput of
print "-".$imgname."-".$imgdbname."-";
seems exactly the same.
-cachepics/c3s9taopkp.png-cachepics/c3s9taopkp.png-
I will go crazy and don’t know why the db version does not work.
Perhaps the problem is that readfromdb have errors and generate warnings, printing these warning to STDIO, adding garbage to the resulting image, or aborting any later header (like content-type).
You could probably check the server error log for that (any warning of the style ‘header can’t be added, because already printed in file..’), or test if both resulting files are the same size.