Reputation: 3893
In my laravel
-application I have a <form>
where it is possible to upload multiple files. When I submit the form the multiple files get stored into the database
, but the table column attachment
, which is supposed to store the path of the file, always displays 1
(true).
if (request()->has('attachment_files')) {
$files = request()->attachment_files;
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filesize = $file->getClientSize();
$path = Storage::disk('local')->put('attachments' . $filename, $extension);
$data = SingleApplicationFile::create([
'files_id' => $application->id,
'single_application_id' => $application->id,
'attachment' => $path,
'attachment_name' => $filename,
'attachment_size' => $filesize,
]);
$attachment_file[] = $data;
new SingleApplicationFile($attachment_file);
}
}
As mentioned, the line $path = Storage::disk('local')->put('attachments' . $filename, $extension);
always gives me true
and in the database column a "1" is stored.
I used this method before for single file upload, and in that case, the mentioned line stores attachments/somefilename.pdf
- so what is the issue here
Upvotes: 4
Views: 9789
Reputation: 2616
put()
returns a boolean value, which is why you are seeing a 1
in your database column and not the string path to your stored file. You might be thinking of putFile()
instead.
Upvotes: 4
Reputation: 9693
Next you need to run
php artisan storage:link
and then you can retrieve the file as
{{ asset('storage/attachments' . $filename) }}
so you may like to save as
if ( Storage::disk('local')->put('attachments' . $filename, $extension) ) {
$path = 'storage/attachments' . $filename;
}
Upvotes: 0