使用PHP glob在FTP服务器上列出文件不起作用 [英] Listing files on FTP server using PHP glob is not working

查看:125
本文介绍了使用PHP glob在FTP服务器上列出文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码访问目录。

  $ location ='files /'; 
$ pictures = glob($ location。* .png);

我想通过FTP访问远程路径

  $ location = opendir('ftp:// user:password @ host_name / files'); 
$ pictures = glob($ location。* .png);

我的FTP目录访问代码无法正常工作。

解决方案

PHP <$ c $


注意:此功能不起作用在远程文件上,因为要检查的文件必须可访问通过服务器的文件系统。







唯一可靠的方法是列出匹配通配符的文件,将使用PHP FTP函数列出所有文件并在本地过滤它们:

  $ conn_id = ftp_connect(ftp.example.com )或死(无法连接); 
ftp_login($ conn_id,username,password)或死(无法登录);
ftp_pasv($ conn_id,true)或死(无法更改为被动模式);

$ files = ftp_nlist($ conn_id,/ path);

foreach($ files as $ file)
{
if(preg_match(/ \.png $ / i,$ file))
{
回显Found $ file \\\
;




(这就是<$ c




$ c> glob
会在内部完成,一些(大多数)FTP服务器将允许你直接使用通配符:

  $ conn_id = ftp_connect(ftp.example .com)或死(无法连接); 
ftp_login($ conn_id,username,password)或死(无法登录);
ftp_pasv($ conn_id,true)或死(无法更改为被动模式);

$ files = ftp_nlist($ conn_id,/path/*.png);

foreach($ files as $ file)
{
echoFound $ file\\\
;
}

但这是一个非标准功能(虽然广泛支持)。 b
$ b

有关详细信息,请参阅我对 FTP目录部分使用通配符列表的答案。


I use this code to access directory.

$location = 'files/';
$pictures = glob($location . "*.png");

I want to access remote path using FTP

$location = opendir('ftp://user:password@host_name/files');
$pictures = glob($location . "*.png");

My FTP directory access code not work correctly.

解决方案

PHP glob function does not support URL wrappers:

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.


The only reliable way is to list files matching a wildcard, is to list all files using PHP FTP functions and filter them locally:

$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");

$files = ftp_nlist($conn_id, "/path");

foreach ($files as $file)
{
    if (preg_match("/\.png$/i", $file))
    {
        echo "Found $file\n";
    }
}

(this is what the glob would do internally anyway, had it supported URL wrappers)


Some (most) FTP servers will allow you to use a wildcard directly:

$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");

$files = ftp_nlist($conn_id, "/path/*.png");

foreach ($files as $file)
{
    echo "Found $file\n";
}

But that's a nonstandard feature (while widely supported).

For details see my answer to FTP directory partial listing with wildcards.

这篇关于使用PHP glob在FTP服务器上列出文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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