如何在HTML页面上显示来自FTP服务器的图像? [英] How to display images from FTP server on HTML page?
问题描述
我尝试为FTP服务器显示图像建立一个图像库。 FTP服务器需要密码认证。我扫描文件成功,但图像不显示在页面上,并通过点击参考页面询问用户名和密码。
$ content ='';
$ ftp_server =255.122.111.111;
$ ftp_user =user_name;
$ ftp_pass =密码;
$ conn_id = ftp_connect($ ftp_server)或死(无法连接到$ ftp_server);
$ b $ if if(@ftp_login($ conn_id,$ ftp_user,$ ftp_pass)){
$ content。=< br />连接为$ ftp_user @ $ ftp_server\\\
;
} else {
$ content。=< br />无法以$ ftp_user \\\
连接;
}
$ files = ftp_nlist($ conn_id,$ dir);
foreach($ files为$ file_name)
{
$ content。='
< div>
< a href =ftp://'.$ftp_server.'/'.'filefile_name。'>
< img src =ftp://'.$ftp_server.'/'.$file_name。'width =150height =150>
< / a>
< / div>';
}
我需要做那些图片是在页面上显示的吗?
您可以准备一个脚本(例如 getimage.php ),根据请求…
在HTML代码中插入通常的 标签。
getimage.php 脚本。手动提取图像类型:
//从FTP服务器获取图像内容到$ binary
// $二进制文件现在包含图像文本
// Then ....
header('Content-type:'。image_file_type_from_binary($ binary));
echo $ binary;
函数image_file_type_from_binary($ binary){
if(
!preg_match(
'/ \\\A(?:( \xff\xd8\xff )|(GIF8 [79]一个)|(\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];
}
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.
$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?
You could prepare a script (e.g. getimage.php) that, upon request…
- gets the image file from the FTP server into a (binary) string variable, like in your script, then
- prepares the image header correctly, like in the snippet below,
(also see this link from stackoverflow) - prints the (binary) image string.
In the HTML code insert the usual tags.
Follows a snippet of the getimage.php script. The image type is extracted manually:
// 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];
}
这篇关于如何在HTML页面上显示来自FTP服务器的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!