将图像从 FTP 检索到网页 [英] Retrieve image from FTP to webpage

查看:31
本文介绍了将图像从 FTP 检索到网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ftp 服务器中有一个文件夹,其中包含多个图像.我正在尝试在

I have a folder in my ftp server which contains several images. I'm trying to access and show these images in a webpage like

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

但它要求输入 FTP 用户名和密码.我怎样才能做到这一点?也可以使用 JSP 做同样的事情吗?

But it asks for an FTP username and password. How can I achieve this? Also is it possible to do the same using JSP?

推荐答案

使用最新版本的网络浏览器(Chrome59Firefox 61),你甚至不能使用 ftp:// 获取图像的 URL.无论如何,这从来都不是一个好的解决方案.

With the latest versions of web browsers (Chrome 59, Firefox 61), you cannot even use ftp:// URL to retrieve an image. 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.

创建一个充当图像源的脚本(PHP 或您使用的任何其他脚本)(您将在 <img src=...> 属性中使用它.脚本将通过从 FTP 服务器下载图像来生成"图像.

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 中实现这样的脚本(比如 image.php)最简单的方法是:

The most trivial way to implement such a script in PHP (say image.php) is:

<?

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

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

(您需要对凭据进行 URL 编码,如果它们包含保留符号 - 您可以使用 urlencode).

然后你在 HTML 中使用它:

And then you use it in the HTML like:

<a src="image.php" />

(假设 image.php 与您的 HTML 页面位于同一文件夹中)

(assuming the image.php is in the same folder as your HTML page)

该脚本使用 FTP URL 包装器.如果您的网络服务器不允许这样做,您必须使用 FTP 功能更加困难.看PHP:如何将文件从 FTP 服务器读取到变量中?

The script uses FTP URL wrappers. If that's not allowed on your web server, you have to go the harder way with FTP functions. See PHP: How do I read a file from FTP server into a variable?

虽然对于一个真正正确的解决方案,您应该提供一些与文件相关的 HTTP 标头,例如 Content-LengthContent-TypeContent-Disposition.为此,请参阅 通过 PHP 脚本将文件从 FTP 服务器下载到带有 Content-Length 标头的浏览器,而无需将文件存储在 Web 服务器上.

Though for a really correct solution, you should provide some HTTP headers related to the file, like Content-Length, Content-Type and Content-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.

实际上,您可能会有更多图像,因此您不会在脚本中硬编码文件名,但脚本会将名称作为参数.
请参阅列出并从 FTP 下载点击的文件

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 检索到网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆