3

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.

2 Answers 2

5

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

is it store in storage folter?
2

Storage::makeDirectory(public_path('uploads/release/'.$reference));

https://laravel.com/docs/7.x/filesystem#directories

Or you can also do

$file->move(public_path('uploads/release/'.$reference), $name);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.