Reputation: 13
i am trying to create a file in laravels App/Http/Controller directory so i had used this code
$path = 'App/Http/Controller/';
$fileName = 'demo.php';
$contents = "<?php echo 'hello'; ?>";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
$path = $path.$fileName;
file_put_contents($path, $contents);
but this is creating a file with directory in Public folder..
how to get rid of this.?
Upvotes: 1
Views: 3117
Reputation: 4291
You can setup a disk at config/filesystems.php
and then benefit from Storage
facade methods.
config/filesystems.php
'tmp' => [
'driver' => 'local',
'root' => public_path('..'),
],
Notice: public_path()
Starts from public
folder, you can go one step ahead using public_path('..')
You can now use Storage
methods such as allDirectories
and allFiles
for retrieving directories and files, or put
for saving new files as thoroughly explained in laravel doc.
Storage::disk('tmp')->allDirectories();
Storage::disk('tmp')->allFiles('routes');
// Storing new photo
Storage::disk('tmp')->put('newfile.jpg',$content);
Upvotes: 3