Melodia's avatar

How can I create a folder for each registration on Amazon s3

I am using Amazon S3 to add files to it for my business registration system.

Inside the store method I have the following:

$input              = $request->all();

$files              = isset($input['file']) ? $input['file'] : array ();
$business_names     = json_decode($input['business_names'], true);
$business_details   = json_decode($input['business_details']);
$share_amount       = json_decode($input['share_amount'], true);
$entity             = json_decode($input['entity'], true);
$directors          = json_decode($input['directors'], true);
$shareholders       = json_decode($input['shareholders'], true);
$appointments       = json_decode($input['appointments'], true);

$input['user_id'] = Auth::user()->id;

Log::info(Auth::user());
Log::info($request->user());
/* Create Business Record */
$business = new Business;
$business->business_names = json_encode($business_names);
$business->share_amount = $share_amount ?: 0;
$business->entity = $entity ?: '';
$business->business_physical_address = json_encode($business_details->physical_address);
$business->has_business_postal_address = $business_details->has_postal_address;
$business->business_postal_address = json_encode($business_details->postal_address);
$business->user_id = $input['user_id'];
$business->save();

/* Upload Files */
foreach($files as $file) {
    if ($file) {
        Storage::disk()->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public');
        // $file->storeAs('files/' . $business->id, $file->getClientOriginalName());
    }
} 

All works until now...but what I want something more organized, in a sense that, every time I save a new business, a folder is also created with that business id and the images should be added to that particular folder.

I tried this:

 Storage::disk('s3')->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public/' . $business->id);

But I see the following error when I try to save a business:

#0 C:\xampp\htdocs\startingabandbaby\vendor\league\flysystem\src\Adapter\Local.php(356): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', 'C:\xampp\htdocs...', 356, Array)

Any way around that?

0 likes
5 replies
MThomas's avatar

You could first create a directory using the designated Storage API.

$directory = 'public/' . $business->id;
Storage::disk('s3)->makeDirectory($directory);

Then put the files in the newly created directory

$directory = 'public/' . $business->id;
Storage::disk('s3')->put($file->getClientOriginalName(), fopen($file, 'r+'));

By the way you can set the default disk in config/filesystems.php, so you don't have to specify it over and over again.

Melodia's avatar

@MTHOMAS - This creates the folder inside my public folder yes, but the files for that particular business are still being added outside that folder.

This is what I have now:

        $directory = 'public/' . $business->id;
        Storage::disk()->makeDirectory($directory);

        foreach($files as $file) {
            if ($file) {
                Storage::disk()->put($file->getClientOriginalName(), fopen($file, 'r+'));                   
            }
        }
MThomas's avatar
MThomas
Best Answer
Level 35

That is due to the use of $file->getClientOriginalName(). That specifies the path where you want the file to be uploaded. You did not specify the path with the directory.

So you could do this:

$directory = 'public/' . $business->id;
Storage::disk()->makeDirectory($directory);

foreach($files as $file) {
    if ($file) {
        Storage::disk()->put($directory . '/' .$file->getClientOriginalName(), fopen($file, 'r+'));                   
    }
}
Melodia's avatar

@MTHOMAS - I see why one of my previous attempts didnt work. I had in a different structure.

Storage::disk('3')->put($file->getClientOriginalName(), fopen($file, 'r+'), 'public/' . $business->id )

Well, I also didnt have the makeDirectory command

Your code works on my storage. Am just going to check now if it also gets added to aws

How would I retrieve the image from a specific folder?

Say, I want to retrive the images from business id number 20

Please or to participate in this conversation.