duandai7601 2018-02-09 07:58
浏览 27

scandir返回dirs但不返回fileq

I'm using scandir to locate the a folder and then check inside for the file I want to use in a link. The where I'm using it to grab the folder is fine but if I try to get the file it comes back 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);

That will dump a NULL but like if I var_dump($dir2) which is just up one level I'll get the folder name correctly.

Edit: The entire file/folder structure is owned by www-data:www-data my server is ubuntu running a LEMP stack. The permissions have been set to 766 for everything aswell.

  • 写回答

2条回答 默认 最新

  • dq1685513999 2018-02-10 07:49
    关注

    To demonstrate the use of some of the Recursive Iterator classes I used the following code to create a directory structure with dummy files.

    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 );
        }
    }
    

    Which gave a directory structure like this: ( 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
    

    And then within each a sub-directory structure like this: ( cmd line )

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

    With each of these folders containing a pseudo pdf file as named above.


    And, hopefully I did not misunderstand, the following uses a recursiveDirectoryIterator in conjunction with a RecursiveIteratorIterator to work through all folders from a given starting point - using the isFile method to test for files rather than directories.

    /* 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;
        }
    }
    

    From this point the generation of links should be straightforward - hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?