Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge a file with "Request" in Laravel?

Tags:

php

laravel

I have a problem in my project which should merge a file with 'Request',i just find that we can use the function of merge to merge the attributes,but how can we merge a file in laravel?thanks so much.

We can use 'merge' to merge some attributes

$request->merge([
            'ticket_id' => '123',
        ]);

But if i want to merge a file?(i find this way is not workable, i can't not receive a file from the Request)

$request->merge([
            'file' => fopen('xxx', 'r'),
        ]);

And the way likes this is not workable,too.

$request->files = fopen('xxx', '');
like image 733
Chufeng Avatar asked Oct 25 '25 05:10

Chufeng


1 Answers

You need to create a new UploadedFile and then add the file to the request bag.

$fileAddress = storage_path().'/file.jpg';
$file = new UploadedFile($fileAddress, 'file');
$request->files->set('file', $file);
like image 140
Mohammad.Kaab Avatar answered Oct 26 '25 19:10

Mohammad.Kaab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!