Laravel文件类型筛选过滤

我在使用laravel的时候,需要对一个目录下面的文件进行筛选,例如只想要**.txt**类型的文件。

一种方法是:
Storage::disk('c-drive')->allFiles();

得到文件数组,然后再遍历进行过滤筛选,这种对于文件较多不是很方便,而且比较慢;

第二种方式:使用php自带的glob函数,进行直接筛选;
  // List files
  $files=glob($path.$match);
   foreach($files as $file){
     $list[]=substr($file,strrpos($file,"/")+1);
   }  

速度快,但是不是很灵活;

第三种方式,比较推荐,使用symfony的Finder包,这个包Laravel本来就自带了,所以直接上方法:
use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach ($finder as $file) {
    // dumps the absolute path
    var_dump($file->getRealPath());

    // dumps the relative path to the file, omitting the filename
    var_dump($file->getRelativePath());

    // dumps the relative path to the file
    var_dump($file->getRelativePathname());
}

#根据时间,名称来筛选文件
$finder->files()->name('*.php');
use Symfony\Component\Finder\Finder;

$s3 = new \Zend_Service_Amazon_S3($key, $secret);
$s3->registerStreamWrapper('s3');

$finder = new Finder();
$finder->name('photos*')->size('< 100K')->date('since 1 hour ago');
foreach ($finder->in('s3://bucket-name') as $file) {
    // ... do something with the file
}

获取文件真实路径和文件内容:

 dd($file, $file->getRealPath(), $file->getContents());

参考链接:
https://secure.php.net/manual/en/function.iterator-to-array.php

https://symfony.com/doc/current/components/finder.html

另外,还可以通过函数直接将文件内容读到一个数组里面去:

实际应用:

    public function renameFiles(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'driver' => 'required|in:c-drive, d-drive, e-drive, f-drive',
            'dir' => 'required|string|max:1024',
            'patten' => 'required|string|max:1024',
            'extension' => 'required|string|max:1024',
        ]);
        if ($validator->fails()) {
            return $this->outPutJson($validator->errors());
        }
        $dir = Storage::disk($driver)->path($dir);
        $finder = new Finder();
        $txt = iterator_to_array($finder->files()->name('*\.txt')->in($dir));
        $names = file(head($txt)->getRealPath(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $needs = Finder::create()->files()->name($patten)->in($dir)->depth(0);
        // dd($needs);
        foreach ($needs as $key => $value) {
            // dd($value->getExtension());
            $a[] = $value->getRealPath();
        }
        natsort($a);
        $a = array_values($a);
        // dd($a);
        foreach ($a as $key => $value) {
            // dd($a, $names);
            if (isset($names[$key])) {
                $res = rename($value, str_replace([' ', '?', '?', ',', ',', '。', ','], '', $dir . '/' . $names[$key] . $extension));
            } else {
                dd('这个名字及之后的文件名称未做更改:' . $value . '请注意');
            }
        }

    }

参考函数:

glob — 寻找与模式匹配的文件路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SHUIPING_YANG

你的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值