如何在HTML页面上显示来自FTP服务器的图像?

时间:2022-05-10 03:41:01

I try to build an image gallery for display images from FTP server. FTP server requires password authentication. I am scan files successful, but image don't display on page and by clicking on reference page ask user name and password.

我尝试为FTP服务器显示图像构建图像库。 FTP服务器需要密码验证。我扫描文件成功,但图像不显示在页面上,点击参考页面询问用户名和密码。

$content = '';
$ftp_server = "255.122.111.111";
$ftp_user = "user_name";
$ftp_pass = "password";

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    $content .= "<br />Connected as $ftp_user@$ftp_server\n";
} else {
    $content .= "<br />Couldn't connect as $ftp_user\n";
}

$files = ftp_nlist($conn_id, $dir);
foreach($files as $file_name)
{
    $content.=  '
         <div>
            <a href="ftp://'.$ftp_server.'/'.$file_name.'">
            <img src="ftp://'.$ftp_server.'/'.$file_name.'"    width="150" height="150">
           </a>
         </div>';
}

What I need to do that images are have been displayed on page?

我需要做什么图像已显示在页面上?

2 个解决方案

#1


1  

You could prepare a script (e.g. getimage.php) that, upon request…

您可以根据要求准备一个脚本(例如getimage.php)...

  • gets the image file from the FTP server into a (binary) string variable, like in your script, then
  • 将图像文件从FTP服务器获取到(二进制)字符串变量,就像在脚本中一样
  • prepares the image header correctly, like in the snippet below,
    (also see this link from *)
  • 正确准备图像标题,如下面的代码段所示(也可以从*看到此链接)
  • prints the (binary) image string.
  • 打印(二进制)图像字符串。

In the HTML code insert the usual tags.

在HTML代码中插入常用标记。

Follows a snippet of the getimage.php script. The image type is extracted manually:

跟随getimage.php脚本的片段。手动提取图像类型:

// Get the image contents from FTP server into $binary
// $binary contains image text now
// Then ....

header('Content-type: ' . image_file_type_from_binary($binary));
echo $binary;

function image_file_type_from_binary($binary) {
  if (
    !preg_match(
        '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(?:\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
        $binary, $hits
    )
  ) {
    return 'application/octet-stream';
  }
  $type = array (
    1 => 'image/jpeg',
    2 => 'image/gif',
    3 => 'image/png',
    4 => 'image/x-windows-bmp',
    5 => 'image/tiff',
    6 => 'image/x-ilbm',
  );
  return $type[count($hits) - 1];
}

#2


0  

The opened FTP connection and cannot anyhow transfer from your server to the client (web browser). While you can add credentials to the FTP URL, would make the credentials a public information this way.

打开的FTP连接无论如何都无法从您的服务器传输到客户端(Web浏览器)。虽然您可以向FTP URL添加凭据,但会以这种方式将凭据作为公共信息。


Better solution is to route the image through your webserver, hiding away not only the credential, but also the original source of the image.

更好的解决方案是通过您的网络服务器路由图像,不仅隐藏凭证,还隐藏图像的原始来源。

Create a script (PHP or any other you use) that acts as the image source (you will use it in the <img src=...> attribute. The script will "produce" the image by downloading it from FTP server.

创建一个充当图像源的脚本(PHP或您使用的任何其他脚本)(您将在如何在HTML页面上显示来自FTP服务器的图像?属性中使用它。脚本将通过从FTP服务器下载来“生成”图像。

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

实现这样一个脚本(比如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. 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?

该脚本使用FTP URL包装器。如果您的Web服务器上不允许这样做,您必须更加努力地使用FTP功能。请参阅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. 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.

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

#1


1  

You could prepare a script (e.g. getimage.php) that, upon request…

您可以根据要求准备一个脚本(例如getimage.php)...

  • gets the image file from the FTP server into a (binary) string variable, like in your script, then
  • 将图像文件从FTP服务器获取到(二进制)字符串变量,就像在脚本中一样
  • prepares the image header correctly, like in the snippet below,
    (also see this link from *)
  • 正确准备图像标题,如下面的代码段所示(也可以从*看到此链接)
  • prints the (binary) image string.
  • 打印(二进制)图像字符串。

In the HTML code insert the usual tags.

在HTML代码中插入常用标记。

Follows a snippet of the getimage.php script. The image type is extracted manually:

跟随getimage.php脚本的片段。手动提取图像类型:

// Get the image contents from FTP server into $binary
// $binary contains image text now
// Then ....

header('Content-type: ' . image_file_type_from_binary($binary));
echo $binary;

function image_file_type_from_binary($binary) {
  if (
    !preg_match(
        '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(?:\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
        $binary, $hits
    )
  ) {
    return 'application/octet-stream';
  }
  $type = array (
    1 => 'image/jpeg',
    2 => 'image/gif',
    3 => 'image/png',
    4 => 'image/x-windows-bmp',
    5 => 'image/tiff',
    6 => 'image/x-ilbm',
  );
  return $type[count($hits) - 1];
}

#2


0  

The opened FTP connection and cannot anyhow transfer from your server to the client (web browser). While you can add credentials to the FTP URL, would make the credentials a public information this way.

打开的FTP连接无论如何都无法从您的服务器传输到客户端(Web浏览器)。虽然您可以向FTP URL添加凭据,但会以这种方式将凭据作为公共信息。


Better solution is to route the image through your webserver, hiding away not only the credential, but also the original source of the image.

更好的解决方案是通过您的网络服务器路由图像,不仅隐藏凭证,还隐藏图像的原始来源。

Create a script (PHP or any other you use) that acts as the image source (you will use it in the <img src=...> attribute. The script will "produce" the image by downloading it from FTP server.

创建一个充当图像源的脚本(PHP或您使用的任何其他脚本)(您将在如何在HTML页面上显示来自FTP服务器的图像?属性中使用它。脚本将通过从FTP服务器下载来“生成”图像。

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

实现这样一个脚本(比如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. 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?

该脚本使用FTP URL包装器。如果您的Web服务器上不允许这样做,您必须更加努力地使用FTP功能。请参阅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. 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.

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