【问题标题】:scandir returns dirs but not fileqscandir 返回目录但不返回 fileq
【发布时间】:2018-02-09 07:58:48
【问题描述】:

我正在使用 scandir 来定位 a 文件夹,然后在里面检查我想在链接中使用的文件。我使用它来获取文件夹的位置很好,但如果我尝试获取文件,它会返回 null。

$path = "/path/to/plugin";
$dir = array_diff(scandir($path), array('..', '.'));
$tmpYear = 0;
$tmpMonth = 0;
foreach($dir as $year){
  if((int)$year > $tmpYear){
    $tmpYear = (int)$year;
  }
}
$path2 = $path."/".$tmpYear;
$dir2 = array_diff(scandir($path2), array('..', '.'));
foreach ($dir2 as $month) {
  if((int)$month > $tmpMonth){
    $tmpMonth = (int)$month;
  }
}
$comPath = $path2."/".$tmpMonth;
$dir3 = scandir($comPath);
foreach ($dir3 as $filename) {
    $finalfile = $filename;
}
var_dump($dir3);

这将转储一个 NULL,但如果我 var_dump($dir2) 仅上升一级,我将正确获取文件夹名称。

编辑:整个文件/文件夹结构归 www-data:www-data 所有,我的服务器是运行 LEMP 堆栈的 ubuntu。所有的权限都设置为 766。

【问题讨论】:

  • 目录结构是什么?是否有像2015,2016,2017 这样的文件夹和子文件夹1,2,3,4...??
  • 是的,很重要。它是 YY/MM/ 文件夹,然后在每个月的文件夹中都有一个 pdf,我想获取它的名称,然后为它生成一个链接。

标签: php scandir


【解决方案1】:

为了演示一些Recursive Iterator 类的使用,我使用以下代码创建了一个包含虚拟文件的目录结构。

function createpath( $path=NULL, $perm=0644 ) {
    if( !file_exists( $path ) ) {
        createpath( dirname( $path ) );
        mkdir( $path, $perm, TRUE );
        clearstatcache();
    }
}


foreach( range( 2000, 2018 ) as $yr ){
    for( $i=1; $i < 13; $i++ ){
        $path=$dir . $yr . '/' . $i;
        createpath( $path );
        file_put_contents( $path .'/pseudo_pdf_file__'.$i.'.pdf.txt', 'Hello World '.$i );
    }
}

它给出了这样的目录结构:( cmd line )

c:\Temp\fileuploads\1>dir /B
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018

然后在每个子目录结构中像这样:( cmd line )

c:\Temp\fileuploads\1\2018>dir /B /OD
1
2
3
4
5
6
7
8
9
10
11
12

这些文件夹中的每一个都包含一个如上所述的伪 pdf 文件。


并且,希望我没有误解,以下使用recursiveDirectoryIteratorRecursiveIteratorIterator 从给定起点遍历所有文件夹 - 使用isFile 方法来测试文件而不是目录。

/* all files found will be added to this array */
$files=array();

$pttn='@(\.pdf)@';

/* the start directory for the search */
$dir='c:/temp/fileuploads/1/';

/* create the directory iterator */
$dirItr=new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::KEY_AS_PATHNAME );

/* use a recursive iterator to iterate through directories */
foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
    /* As it is files you wish to find, use `isFile()` method */
    if( $info->isFile() ){

        /* Match file name against basic patter to test for .pdf files */
        preg_match( $pttn, $info->getFileName(), $col );

        /* Add matching files to the output array */
        if( !empty( $col ) ){
            $files[]=str_replace( realpath( $dir ),'', realpath( $info->getPathName() ) );
        }
    }
}
/* If any files were found, process the output array */
if( !empty( $files ) ){
    foreach( $files as $file ){
        /* do stuff with $file */
        echo $file;
    }
}

从这一点开始,链接的生成应该很简单——希望这会有所帮助。

【讨论】:

  • 这是一种更好的处理方式,我想我会尝试重写它以使用那些递归迭代器。事实证明,我的代码无法正常工作的原因是该月的文件名是“01”,而我将其类型转换为 int,从而去掉了 0。
【解决方案2】:

原来我将月份转换为 int 并且删除了文件夹名称中的前导 0。

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多