lundi 30 décembre 2019

Laravel - Get list of files in directory, no such file or directory

I am trying to get a list of all the files in the directory resources/layouts/layouts/ where all my layouts are located. In my controller I can do something like this:

        $dir = "../resources/views/layouts/layouts";
        $list = scandir($dir,1);
        $newlist = array_splice($list, -2);

        foreach($list as $item) {
            $dir = "../resources/views/layouts/layouts/";
            $filepath = $dir . $item;
            $content = file_get_contents($filepath);
        }

And this just returns a list of the names and content of the files in that directory like no problem. But when I try to do the same in my LayoutSeeder file and try to populate the database with the data I get the error

failed to open dir: No such file or directory on the line $list = scandir($dir,1);

Code

public function run()
    {
        $dir = "../resources/views/layouts/layouts";
        $list = scandir($dir,1);
        $newlist = array_splice($list, -2);

        foreach($list as $item) {
            $dir = "../resources/views/layouts/layouts/";
            $filepath = $dir . $item;
            $content = file_get_contents($filepath);

            DB::table('layouts')->insert([
                'name' => strtok($item, '.'),
                'content' => $content,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ]);
        }
    }

Why does it work in my controller but not in my seeder? I'm only one level away from the base directory so ../ should do the trick like it does in the controller. But it does not work



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire