Ad

How To Get Files Using Laravel?

- 1 answer

I have a simple function in my controller to get certain files, here is my solution

Here is my folder structure.

enter image description here

codetemplate is the file I am trying to get

my function in a controller to get a file.

   public function code($code_id)
     {
         $settings = Setting::find($code_id);

         $templatefile = file_get_contents("pages.settings/codetemplate.js");

        file_put_contents("pages/codes/" .$code_id .".js", $replacedfile );

         return view('pages.settings.code', compact('settings'));

     }

Now when I run my app I am getting the following error.

ErrorException (E_WARNING)
file_get_contents(pages.settings/codetemplate.js): 
failed to open stream: No such file or directory

What is wrong with my code?

Ad

Answer

Since the file is under the /resources directory, you can use the resource_path() helper.

$templatefile = resource_path('views/pages/settings/codetemplate.js');

or seeing what you are doing in your code:

$templatefilecontent = file_get_contents( resource_path('views/pages/settings/codetemplate.js') );
Ad
source: stackoverflow.com
Ad