doujiao1949 2017-08-14 17:32
浏览 82
已采纳

获取具有修改日期和大小的大型文件列表的高效方法

I am using PHP 5.6 on Linux. I am getting a list of files and directories in a directory

$directory_listing = scandir($path);

I know I could do something similar to following to get an array of the filenames plus modified date and size.

$listings_final = array();
foreach ($directory_listing as $listing) {
    $listings_final[]['listing_name'] = $listing;
    $listings_final[]['listing_modified'] = filemtime($listing);
    $listings_final[]['listing_size'] = filesize($listing);
}

This will be slow when the number of files is extremely large. Is there a more performant way to archive this?

  • 写回答

1条回答 默认 最新

  • dongque1646 2017-08-15 20:05
    关注

    Not likely. In accessing the filesystem, the metadata for each file is grabbed per file, not in bulk. Even relatively low-level tools like ls do it this way. So if you have 10,000 files in a directory, there's not going to be any way around making 10,000 separate calls to grab the metadata for all of the files. There may be some tool that purports to get it all in one go, but it's just going to be abstracting the fact that it's looping through all files in a directory to get the information, and isn't going to be any faster.

    The only way I know of to do this would be to create something that accesses the filesystem directly for this information. However, that would not be recommended, as it makes your application then dependent on specific filesystems (i.e. ext4).

    Your best bet is going to be to grab the initial file listing for the directory and populate that, since that can be done quickly. Then, asynchronously, grab the metadata for each file and populate your application. This way, the user can begin interact with your application without having to wait for all of the metadata to load.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法