微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在laravel中基于created_at获取文件?

如何解决如何在laravel中基于created_at获取文件?

文件夹中有多个文件,我正在获取文件以将它们合并。现在,我希望它们在创建日期的基础上以升序获取这些文件吗?首先创建哪个文件应该首先进入循环迭代。

$bulk_pdf = public_path('bulk-pdf-consignment/manifest' . $this->manifest_id . '_.pdf');
$allFilesPath = public_path('bulk-pdf-consignment/manifest_' . $this->manifest_id);
$filesInFolder = File::files($allFilesPath);
 
foreach ($filesInFolder as $path) {
         $file = pathinfo($path);
         $finalPath = $file['dirname'] . '/' . $file['basename'];
         $pdfMerger->addPDF($finalPath,'all');
 }

解决方法

使用过的laravel集合sortBy方法

$bulk_pdf = public_path('bulk-pdf-consignment/manifest' . $this->manifest_id . '_.pdf');
 $allFilesPath = public_path('bulk-pdf-consignment/manifest_' . $this->manifest_id);

$filesInFolder = collect(File::files($allFilesPath))->sortBy(function($file) {
  return $file->getCTime();
})

foreach ($filesInFolder as $path) {
        $file = pathinfo($path->getPathName());
        $finalPath = $file['dirname'] . '/' . $file['basename'];
        $pdfMerger->addPDF($finalPath,'all');
}

查看如何在splfileinfo类中获取文件名和目录名

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。