Alexander
Alexander

Reputation: 290

Laravel Storage not finding file

Laravel is not finding my file. I have a file with the following path:

C:\xampp\htdocs\IRB Source\storage\app\documents\Consent_Document.docx

I am trying to copy this file into a new file. I have the following in my controller:

File::copy(storage_path('app/documents/' . $document), storage_path('app/project/' .$id. '/' . $document));

When I debug with the following command:

dd(storage_path('app/documents/' . $document))

I get the proper path above. But Laravel is saying no file exists.

Upvotes: 0

Views: 83

Answers (1)

glinda93
glinda93

Reputation: 8479

That's because directory app/project/{$id} is not created yet.

You can ensure directory exists before copying as the following way:

$file = storage_path("app/documents/{$document}");
$dir = storage_path("app/project/{$id}");
if (!File::isDirectory($dir)) {
  File::makeDirectory($dir, 0755, true, true);
}
File::copy($file, "{$dir}/{$document}");

Upvotes: 1

Related Questions