简体   繁体   中英

Get part of path of all files within some directory and subdirectories

I am retrieving all files using 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. 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

Expected output: 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?

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 "."

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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