KHAN's avatar
Level 4

[File Storage Help] Create Personalised Directory/Rename And Move

Hi i have a user story where a user uploads there image, then a personalised directory gets created for this specific user. The users image gets renamed to "photo" and then moved into there personalised directory. How can i do this. Here is what i have so far

$usersDirectory  = Storage::makeDirectory($user->id);
if($request->hasFile('user_image')
{
$userImage = $request->file('user_image');

}
0 likes
2 replies
KHAN's avatar
Level 4

@ohffs Im having a problem where it can't find the newly moved file but i can see it there in my IDE.

  1. Create New Directory With User ID within App/Storage/Attachments as a subfolder
  2. Move uploaded file to new directory and rename
  3. Get the file using Storage::get
  4. Store details such as name, size and path within an attachment resource
        Storage::makeDirectory($user->id);
        $attachment_storage = 'attachments';
        $storage_path = storage_path() . '/app/' . $attachment_storage;
        if ($request->hasFile('image')) {

            $storage_path = $storage_path . '/' . $user->id . '/';
            $image = $request->file('image');
            $image->move($storage_path, 'photo' . '.' . $image->getClientOriginalExtension());

            $mage = Storage::get($attachment_storage . '/'. $user->id . '/' . 'photo' . '.'. $image->getClientOriginalExtension());
            
            $attachment = new Attachment();
            $attachment->name = $image->getFilename();
            $attachment->type = $image->getMimeType();
            $attachment->path = $image->getPath();
            $attachment->size = $image->getSize();
            $attachment->save();
        }

Please or to participate in this conversation.