John Davidson

php - file_put_contents to create file out of public directory in laravel

0 comments
Message:


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



My Answer:
"";__ file_put_contents($path . $fileName, $contents);__However, this code will not work as expected because the App/Http/Controller directory is not writable by the web server. Laravel's public directory is the only directory that is publicly accessible by the web server.

If you want to create a file outside of the public directory, you can use the storage directory which is typically located at storage/app. You can use the storage_path() helper function to get the full path to the storage directory.

Here is an example of how you can create a file in the storage directory:

php
$path = storage_path('app/demo.php');
$contents = "";

file_put_contents($path, $contents);


This code will create a file named demo.php in the storage/app directory with the contents "".

Rate this post

5 of 5 based on 8517 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us