简体   繁体   English

从 FTP 检索图像到网页

[英]Retrieve image from FTP to webpage

I have a folder in my ftp server which contains several images.我的 ftp 服务器中有一个文件夹,其中包含多个图像。 I'm trying to access and show these images in a webpage like我正在尝试在网页中访问和显示这些图像

<img src="ftp://my_ftp_ip_address/Images/imagename.jpg"/>

But it asks for an FTP username and password.但它要求提供 FTP 用户名和密码。 How can I achieve this?我怎样才能做到这一点? Also is it possible to do the same using JSP?也可以使用 JSP 做同样的事情吗?

With the latest versions of web browsers ( Chrome 59 , Firefox 61 ), you cannot even use ftp:// URL to retrieve an image.使用最新版本的网络浏览器( Chrome 59Firefox 61 ),您甚至无法使用ftp:// URL 来检索图像。 And it was never a good solution anyway.无论如何,这从来都不是一个好的解决方案。


The correct solution is to route the image through your webserver, hiding away not only the credentials, but also the original source of the image.正确的解决方案是通过您的网络服务器路由图像,不仅隐藏凭据,而且隐藏图像的原始来源。

Create a script (PHP or any other you use) that acts as an image source (you will use it in the <img src=...> attribute. The script will "produce" the image by downloading it from the FTP server.创建一个脚本(PHP 或您使用的任何其他脚本)作为图像源(您将在<img src=...>属性中使用它。该脚本将通过从 FTP 服务器下载图像来“生成”图像。

The most trivial way to implement such a script in PHP (say image.php ) is:在 PHP 中实现这样一个脚本的最简单的方法(比如image.php )是:

<?

header('Content-Type: image/jpeg');

echo file_get_contents('ftp://username:password@ftp.example.com/path/image.jpg');

And then you use it in the HTML like:然后你在 HTML 中使用它,如:

<a src="image.php" />

(assuming the image.php is in the same folder as your HTML page) (假设image.php与您的 HTML 页面在同一文件夹中)


The script uses FTP URL wrappers .该脚本使用FTP URL 包装器 If that's not allowed on your web server, you have to go the harder way with FTP functions.如果您的 Web 服务器不允许这样做,则您必须更努力地使用 FTP 功能。 See PHP: How do I read a file from FTP server into a variable?请参阅PHP:如何将文件从 FTP 服务器读取到变量中?


Though for a really correct solution, you should provide some HTTP headers related to the file, like Content-Length , Content-Type and Content-Disposition .尽管对于真正正确的解决方案,您应该提供一些与文件相关的 HTTP 标头,例如Content-LengthContent-TypeContent-Disposition For this, see Download file via PHP script from FTP server to browser with Content-Length header without storing the file on the web server .为此,请参阅通过 PHP 脚本从 FTP 服务器将文件下载到带有 Content-Length 标头的浏览器,而无需将文件存储在 Web 服务器上


In practice, you will likely have more images, so you will not have the file name hard-coded in the script, but the script will take the name as a parameter.实际上,您可能会有更多的图像,因此您不会在脚本中硬编码文件名,但脚本会将名称作为参数。
See List and download clicked file from FTP查看列表并从 FTP 下载单击的文件

You are telling the browser to access the images using the ftp protocol, this is why it is trying to login.您告诉浏览器使用 ftp 协议访问图像,这就是它尝试登录的原因。

Change ftp:// to http:// (or https:// if using ssl) to access the images.将 ftp:// 更改为 http://(如果使用 ssl,则为 https://)以访问图像。

You can offcourse also add the username and password to the image, but this way everyone can look at your code and login to your ftp server... not really secure ;)您当然也可以将用户名和密码添加到图像中,但这样每个人都可以查看您的代码并登录到您的 ftp 服务器......不是很安全;)

您可以尝试像这样修改您的 src 属性:

<img src="ftp://username:password@my_ftp_ip_address/Images/imagename.jpg"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM