Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to create a folder in public path dynamically when a file is uploaded?

I have created a function which stores file path in database and stores the file in public/uploads/release folder. Here is my Controller:

$reference = $request->get('reference');
    $uploads = array();
    if($files = $request->file('uploads'))
    {  
    foreach ($files as $file) {
        $name = $file->getClientOriginalName();
        $file->move('uploads/release', $name);
        $uploads[] = $name;
    }

Here I am storing file in public/uploads/release folder, but now I want to create a folder dynamically every time a file is uploaded, and the folder must be the value from the $reference variable. e.g: reference is 'ABC123'. Then the file must be automatically stored in public/uploads/release/ABC123 folder.

like image 895
Kundan Nasir Avatar asked Jan 29 '26 08:01

Kundan Nasir


1 Answers

You can use File Class in laravel

use Illuminate\Support\Facades\File;

$path = public_path().'/uploads/release/'.$request->get('reference');
if (! File::exists($path)) {
    File::makeDirectory($path);
}
like image 120
Tirdad Abbasi Avatar answered Jan 31 '26 22:01

Tirdad Abbasi



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!