简体   繁体   English

使用RecursiveIteratorIterator显示所有子文件和地图

[英]Using the RecursiveIteratorIterator to show all the child files and maps

I am currently working with the RecursiveIteratorIterator and the RecursiveDirectoryIterator to look through all the folders and files in my project folder (it starts from the project root folder). 我目前正在与RecursiveIteratorIterator和RecursiveDirectoryIterator一起浏览项目文件夹中的所有文件夹和文件(它从项目根文件夹开始)。

The point of this tool is to find all the file differences. 该工具的目的是找到所有文件差异。 The problem is, it works on my localhost but not on the server nor the systems of my colleague's. 问题是,它可以在我的本地主机上运行,​​而不能在服务器或同事的系统上运行。


I am on PHP 7.1.0, just like my colleague's 我使用的是PHP 7.1.0,就像我同事的一样

I am using the following code for the iterator 我正在为迭代器使用以下代码

new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $startingPoint ), RecursiveIteratorIterator::SELF_FIRST );

the '$startingPoint' is the root location of the project. “ $ startingPoint”是项目的根目录。

The full code is 完整的代码是

$startingPoint = realpath(__DIR__ . '../../'); # project root

#uncomment for list of files *down here*
$objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $startingPoint ), RecursiveIteratorIterator::SELF_FIRST );

foreach( (array) $objects as $name => $object  ) {
    if( strpos( $object->getPathName(), '.git' ) == FALSE && strpos( 
       $object->getPathName(), '.idea' ) == FALSE && strpos( $object->getPathName(), 'uploads' ) == FALSE && ! ( $object->getBaseName() == '.' || $object->getBaseName() == '..' ) ) {

    $fileLocation = str_replace( array( $startingPoint . '\\', '\\' ), array( '', '/' ), $object->getPathName() );

    #uncomment when generating a new list of directories
//                $filesShouldExist[] = [ "loc" => $fileLocation, "size" => $object->getSize() ];
    }
}

It will just stop at the creation of the iterators, and no errors will be given. 它只会在创建迭代器时停止,并且不会给出任何错误。

Please note, this code works on (my) windows, however, not on mac. 请注意,此代码可在(我)的Windows上运行,但不适用于Mac。

From PHP's documentation : PHP的文档中

__DIR__ The directory of the file. __DIR__文件目录。 If used inside an include, the directory of the included file is returned. 如果在include中使用,则返回包含文件的目录。 This is equivalent to dirname(__FILE__). 这等效于目录名(__FILE__)。 This directory name does not have a trailing slash unless it is the root directory. 除非它是根目录,否则此目录名称不带斜杠。

So, add a slash to the path: 因此,在路径中添加一个斜杠:

$startingPoint = realpath(__DIR__ . '/../../'); # note the added slash at the start of the string literal

and see if that solves your problem. 看看是否能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM