Broken image with readfile and file_get_contents

2019-01-28 02:19发布

I'm trying to use a function in order to get this working:

<img src='login.php?image=ind_legend.jpg'>

But I can't pass through the function to place the image. I went back a couple of steps and tried only this part of the code:

<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
?>

or using this function:

echo file_get_contents($source);

but the fact is that the only thing I get is a broken image cross (IE) or nothing in Firefox.

I would appreciate any suggestions

Thanks in advance

4条回答
等我变得足够好
2楼-- · 2019-01-28 02:22

You certainly have some whitespace in your PHP script, or a UTF-8 BOM invisibly before your <?php opening marker. Use a hexeditor to find out.

To debug it further, open the image URL http://localhost/login.php?image=ind_legend.jpg directly in your browser, save the file. And then compare it to the original JPEG.

查看更多
Animai°情兽
3楼-- · 2019-01-28 02:24

use the ob_clean() function of php before the readfile()

查看更多
来,给爷笑一个
4楼-- · 2019-01-28 02:36

first of all point your browser to http://youraddress/login.php?image=ind_legend.jpg and check the result.

Maybe the file /sales/test.jpg is corrupted or you don't have enabled the http:// wrapper for readline

At last save the corrupted image via the save image as... context menu option of your browser of choice and try to open it with a text editor. I will not surprised if you will find an error message (if you have them enabled).

查看更多
疯言疯语
5楼-- · 2019-01-28 02:47

As previously mentioned, you probably have some whitespace. I'd try replacing the entire file with the code below. Removing the closing php statement eliminates any chance that there is extra whitespace:

<?php
$file = "http://localhost/sales/test.jpg";
header('Content-type: image/jpeg');
readfile($file);
查看更多
登录 后发表回答