这个问题在这里已经有了答案:
Retrieve image from FTP to webpage
(3 个回答)
1年前关闭。
我在尝试在另一个网站内显示我的 FTP 服务器上的图片时遇到了巨大的麻烦。到目前为止,我一直在尝试的是在 iFrame 中显示图像。我的代码如下:
<body>
<?php
$pic = file_get_contents('ftp://username:password@servername.net/image.jpg');
echo "<iframe src=". $pic . "> </iframe>";
?>
</body>
当被问到这个问题时,这基本上是似乎对其他用户有效的代码,但对我来说,它显示了一个带有以下消息的 iframe:Not Found
The requested URL was not found on this server. Apache/2.4.29 (Ubuntu) Server at servername.net Port 1234
我真的不知道该尝试什么了,因为我已经尝试了几乎所有我找到的解决方案,所以我希望你们中的一个人知道如何帮助我。
最佳答案
$pic 包含图像数据。因此,您需要将其编码为图像。 此选项可能会降低旧浏览器的速度。
您应该在标签中显示图像。您可以更好地结合 HTML 和 PHP:
<img src="data:image/jpeg;Base64,<?=base64_encode(file_get_contents('ftp://username:password@servername.net/image.jpg'));?>">
如果图像不可用,您可以尝试清除浏览器缓存。 如果您将随机参数添加到 url,它将每次更新。我不推荐它,但如果你愿意,你可以这样做:<img src="data:image/jpeg;Base64,<?=base64_encode(file_get_contents('ftp://username:password@servername.net/image.jpg?_t='.time()));?>">
关于php - 显示来自网站内 ftp 服务器的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64515151/