简体   繁体   English

从目录和子目录php获取所有文件,大小,路径

[英]Fetch all files,size,path from directories and subdirectories php

I want to fetch all the file names, size, path from all the directories and subdirectories using a function. 我想使用函数从所有目录和子目录中获取所有文件名,大小,路径。 I have already done the same using recursive function. 我已经使用递归函数做了同样的事情。 Here is my code. 这是我的代码。

function scan_dir($path, $exclude){

$ite = new RecursiveDirectoryIterator($path);
$bytestotal=0;
$nbfiles=0;

$file_data = array();

$i= 0;
foreach (new RecursiveIteratorIterator($ite,RecursiveIteratorIterator::CHILD_FIRST) as $filename => $cur) {


if ($cur->isDir())
    {
        continue ;
    }

    $files = str_replace('\\','/', $filename);
     $filesize = $cur->getSize();

    $exclude = array_filter($exclude);


     $data = array('name'=>$files, 'path'=>$cur->getPath(),'size'=>$cur->getSize());
     $file_data[] = $data;  
    $i++;
    $nbfiles++;

    $bytestotal += $filesize;       
 }
  $bytestotal = $bytestotal;
  return array( 'total_files'=> $nbfiles, 'total_size' => $bytestotal, 'files'=> $file_data);
}

The problem is When i run my code on linux server, due to the recursive function, it runs slow. 问题是当我在Linux服务器上运行代码时,由于具有递归功能,它运行缓慢。 Please can i have alternate for the same so as I can get the same output but use the different function. 请为我提供相同的替代项,以便获得相同的输出但使用不同的功能。

I want to add a if else part ie check the server, if it is linux then { } else { recursive function } Any help is really appreciated. 我想添加一个if else部分,即检查服务器, 如果它是linux,{} else {递归函数}任何帮助都非常感谢。

Use glob 使用glob

$folder = dirname( __FILE__ ) ."./images/*";
foreach( glob( './images/*.*' ) as $filename ) {
    echo $filename;
    echo $size = filesize($filename);
}

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

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