通过Linux上的Php对共享Windows驱动器上的文件进行计数 [英] Counting files on a shared windows drive via Php from Linux

查看:83
本文介绍了通过Linux上的Php对共享Windows驱动器上的文件进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows共享文件夹中具有扩展名为.msg的文件,而我的php服务器是Linux(LAMP服务器).我正在尝试编写一个PHP脚本,该脚本只计算Windows共享文件夹上的.msg文件的数量.

I have files with .msg extension on a windows shared folder and my php server is Linux (LAMP server). I am trying to write a php script which simply counts the number of .msg files on the Windows shared folder.

我正在使用smbclient类,这是我写的:

I am using smbclient class and this is what I wrote:

<?php
require_once ('smbclient.php');
$smbc = new smbclient ('//192.168.10.14/reservations', 'user', 'pass');
$handle = popen ($smbc);
$files = glob($handle . '*.msg');
$filecount = count( $files );
echo $filecount;
?>

但是,我总是得到0作为输出,但是有200多个文件.

However, I am always getting 0 as output, but there are over 200 files.

推荐答案

您不能glob这样的句柄.您正在有效地尝试遍历Resource (12)/*.msg,如果它是popen返回的实际资源(这意味着smbclient::__toString()将需要返回protocol://username:password@host/the/share/url并且需要自动注册了一个流包装器)表示protocol).

You cant glob a handle like that. You are effectively trying to glob Resource (12)/*.msg if it is an actual resource returned by popen (meaning that smbclient::__toString() would need to return protocol://username:password@host/the/share/url and would have needed to automagically registered a stream wrapper for protocol).

但是即使那样,它也不起作用,因为glob仅适用于文件系统中存在的内容(因此它实际上需要挂载)...似乎SPL的GlobIterator也是这种情况.

But even then it wouldn't work because glob only works with things existent in the filesystem (so it would need to actually be mounted)... Seems to also be the case with SPL's GlobIterator.

至少,您需要遍历每个文件,并根据您的模式检查名称.因此请记住,根据网络连接和共享上文件/目录的数量,此时任何解决方案的速度都会变慢.

At a minimum, you will need to traverse every file and check the name against your pattern. So keep in mind any solution at this point is going to be somewhat slow depending on the network connection and number of files/dirs on the share.

由于我不知道您正在使用的实现的smb客户端的代码,请举一个我确实知道如何使用的示例,它可以工作. munkie/samba 是一个PHP SMB客户端,并且是系统smbclient的相应流包装器,因此您需要使用与流一起使用的文件系统功能来利用它:

Since I dont know the code for the smb client you implementation you are using ill give you an example with one i do know how to use, and that works. munkie/samba is a PHP SMB Client and corresponding stream wrapper for system smbclient so you will need to use filesystem functions that work with streams to utilize it:

使用 SPL迭代器,我们可以简化递归读取目录和搜索文件名:

Using SPL iterators we can make short work of recursively reading directories and searching against filenames:

// assuming you installed via composer so use its autoloder
$autoloader = require_once('vendor/autoload.php');

use Samba\SambaStreamWrapper;

SambaStreamWrapper::register();

$dir = new RecusrsiveDirectoryIterator(new DirectoryIterator(
    'smb://username:password@192.168.10.14/reservations'
));

$finder = new RegexIterator($dir, '#^.+\.msg$#i', RecursiveRegexIterator::MATCH);

$count = 0;

foreach($finder as $match) {
   $count++;
}

echo $count;

使用非常方便 symfony/finder组件

symfony/finder组件使事情变得简单一些,并且不太神秘.除了支持glob和正则表达式搜索模式外,它还实现了Countable,因此我们可以调用$var->count()来获取计数,而不是循环遍历结果并对其进行手动计数(尽管在内部它仍然需要遍历结果以进行计数).它还使更复杂的搜索更易于使用.听起来现在还不是您需要的支持,但是稍后可能会出现:

Using the VERY handy symfony/finder component

The symfony/finder component makes things a bit easier on us and less cryptic. In addition to supporting globs and regex search patterns it implements Countable so we can call $var->count() to get the count instead of looping over the results and counting them manually (though internally it still needs to iterate over the result to count). It also makes much more complex searches easier to work with. Doesn't sound like that is support you need at the moment, but it might come in hand later:

// assuming you installed via composer so use its autoloder
$autoloader = require_once('vendor/autoload.php');

use Samba\SambaStreamWrapper;
use Symfony\Component\Finder\Finder;

SambaStreamWrapper::register();

$finder = new Finder();
$finder
    ->files()
    ->name('*.msg')
    ->in('smb://username:password@192.168.10.14/reservations');

// YAY Countable!
echo $finder->count();

您在此处拥有的另一种选择是实际安装共享,然后使用glob()GlobIteratorsymfony/finder.但这可能会有些棘手,具体取决于您将其用于什么的性质以及如何安装它... 它在这里被钻了一下.

The other alternative you have here is to actually mount the share and then use glob(), GlobIterator, or symfony/finder. But that can get a bit tricky depending on the nature of what you are using this for and how you want to go about mounting it... Its been delved into a bit here.

最后,如果这些是邮箱中的电子邮件,则使用 查看全文

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