5

I want to upload an array of files,in Laravel , and I am not sure what is the path and who to store the file. Eight now the data are stored ,but in my case the path is #. In the image below I have the data that I am sending from front (Vuejs and I am using vue-upload-component)

enter image description here

$fileName = [];

foreach($request->input('files') as $files){
       $contractFile = new ContractFile();
       $contractFile->fill([
           'contract_id'   => $contract->id,
           'name'          => $files['name'],
           'path'          => '#',
       ])->save();
}

ContractFile

class ContractFile extends Model
{
    protected $fillable = ['path','contract_id','name'];

    public function contract()
    {
        return $this->belongsTo(Contract::class);
    }

}

ContractFile db

    Schema::create('contract_files', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('contract_id');
    $table->string('path');
    $table->string('name');
    $table->timestamps();
});

filesystems.php

'uploads' =>[
    'driver' => 'local',
    'root' => storage_path().'file/uploads',
],

2 Answers 2

3

You may use

foreach($request->file('files') as $uploadedFile){

      $filename = time() . '_' . $uploadedFile->getClientOriginalName();

       $path = $uploadedFile->store($filename, 'uploads');

       $contractFile = new ContractFile();
       $contractFile->contract_id = $contract->id;
       $contractFile->name = $uploadedFile->getClientOriginalName();
       $contractFile->path = $path;
       $contractFile->save();

}

By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

To create the symbolic link, you should use the storage:link Artisan command:

php artisan storage:link
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, but I got this. Call to a member function getClientOriginalName() on array
Could you dump($request->input('files')) ??
did you tried $request->files instead of $request->input('files')
No errors now, but the data is not stored, neither data in db.
Pls add your contractFile table schema and ContractFile Model
|
0

In form add file input something like:

<input type="file" multiple name="attachments[]">

Then in controller action handle an array of uploaded files like:

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

// Check if there are files uploadeded for the form name attribute.
if ($request->files->has('attachments')) {
    // Here we get the uploaded fileBag by attribute.
    $fileBag = $request->files->get('attachments');

    // Directory path where to upload the files.
    $dirPath = 'attachments'

    // Loop over each file in the bag to store.
    foreach ($fileBag as $uploadedFile) {
        // Create unique file name. You can write your logic but recommend 
        // using uuid to avoid name collision. Make sure to add same 
        // extension as uploaded file.
        $fileName = (string) Str::uuid() . '.' . $uploadedFile->getClientOriginalExtension();

        // Store using Storage Facades.
        // Note: storing using request->file->store() will clear files array 
        // after first store call and since we are looping here that won't 
        // work.
        $path = Storage::putFileAs($dirPath, $uploadedFile, $fileName);
        
        // Now you can use the path and store it in the DB table or any way 
        // you want.
    }
}

For more configuration refer: Laravel 7.x File Storage, Symfony File Bag, Symfony Uploaded File

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.