我有一个文件在磁盘上的一个文件夹中,我不想从Web浏览器可见
所以我创建了一个image.php文件,它需要一个文件名,并且必须在图像的浏览器中“做echo”。
示例:
mysite.com/image.php?name=selection.png
必须在浏览器中向用户显示图像。
实际上浏览器要求我保存。我想显示它,而不是要求下载...
我正在使用这个片段
header("Content-Type: image/png");
header('Content-Length: ' . filesize($full_file_name));
header("Content-Disposition: inline; filename='$image'");
readfile($full_file_name);
exit(0);
如果保存并打开,img是完美的.如何不要求下载它?
编辑
- 是的,我需要一些东西来使用到图像标签
- 你们大多数人都在回答我的问题,告诉我关于图像创建,但我的问题是所有关于不同的模式告诉浏览器显示的东西,并要求用户保存的东西.没有更多的关于图像创建的回复-你偏离主题!*
- 可能我的问题,我的标题,不清楚 *,但我的问题很简单:
-如何告诉浏览器显示图像,而不是要求用户保存它
图像创建正在工作
通过imag src的图像使用正在工作
**但是当用户点击图片时,会打开一个新的标签页,其中包含图像源作为链接,因为我需要在浏览器中显示全尺寸图像。在这种情况下,浏览器要求我保存!
编辑:
移除
header("Content-Disposition: inline; filename='$image'");
不会改变Firefox的行为
5条答案
按热度按时间ukxgm1gy1#
尝试:
如果您使用的是用户输入,请非常小心使用passthru命令,因为此函数将执行您传递给它的任何内容。
https://www.php.net/manual/en/function.passthru.php
允许将用户提供的数据传递给此函数时,请使用escapeshellarg()或escapeshellcmd()确保用户无法欺骗系统执行任意命令。
cngwdvgl2#
您可以使用PHP的GD库。
in your image.php.
q5iwbnjs3#
要做到这一点,你必须对图像进行base64编码,然后输出结果。你可以使用这样的函数:
然后输出结果作为图像的源,如:
kyks70gy4#
你也可以使用PHP的imagepng:
imagepng -将PNG图像输出到浏览器或文件
dl5txlt95#
如果你的图片路径存储在数据库中,这个方法是可以的。(我用MySQLi做了个例子,让它适应你的数据库API)
如果您使用URL参数选择图像,则上述方法是最佳方法
否则:
可能是解决办法
手册在这里:
http://php.net/manual/en/function.imagepng.php