简体   繁体   English

获取某些目录和子目录中所有文件的部分路径

[英]Get part of path of all files within some directory and subdirectories

I am retrieving all files using Illuminate\\Support\\Facades\\File :我正在使用Illuminate\\Support\\Facades\\File检索所有文件:

    $files = File::allFiles(base_path('resources/views/emails'));
    foreach($files as $file) {
        $file_name = $file->getBasename('.blade.php');
        $path_info = $file->getPathInfo();

        $template = substr($path_info, strpos($path_info, 'emails/') + 7).'.'.$file_name;
        $template = str_replace('/', '.', $template);
    }

Inside emails directory there are more subdirectories with some containing another directory inside.emails目录中有更多子目录,其中一些包含另一个目录。 I would like to get all file names, without extension, but with names of subdirectories the file is stored in. I would also like to omit everything before emails directory (including emails itself) from path name.我想获取所有文件名,不带扩展名,但包含存储文件的子目录的名称。我还想从路径名中省略电子邮件目录(包括电子邮件本身)之前的所有内容。

Example: resources/views/emails/subfolder_one/subfolder_two/myfile.blade.php示例: resources/views/emails/subfolder_one/subfolder_two/myfile.blade.php

Expected output: subfolder_one.subfolder_two.myfile预期输出: subfolder_one.subfolder_two.myfile

The way I did it works, but seems too complicated.我这样做的方式有效,但似乎太复杂了。 Is there a function which would reduce number of lines of code withing foreach loop?是否有一个函数可以减少foreach循环的代码行数?

if it's always your folder start from emails you may explode the path to the array and for loop at index 3 and concatenate result with "."如果您的文件夹总是从emails开始,您可能会在索引 3 处分解数组和 for 循环的路径,并将结果与"."连接起来"."

$str = 'resources/views/emails/subfolder_one/subfolder_two/myfile.blade.php';
$str_arr = explode('/', $str);
$template = '';
for ($i = 3; $i < count($str_arr); $i++) {
    $template .= $str_arr[$i] . '.';
}
dd($template);

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

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