shafee's avatar

Laravel 5.1 file upload same name for folder and database

My controller code where i store the file name into a database table and also moving the file to a folder.

The issue is that i am storing the original name of a file in database table, in contrast i am moving files with uniqueid() and time() . It will arise issues in future. because in database table file name and moved file are with different names.


if(Input::hasFile('profile_pic')){
            $pic = Input::file('profile_pic');
            $mobile->photo1 = $pic[0]->getClientOriginalName();
            $mobile->photo2 = $pic[1]->getClientOriginalName();
            $mobile->photo3 = $pic[2]->getClientOriginalName();
            $mobile->photo4 = $pic[3]->getClientOriginalName();
            $mobile->photo5 = $pic[4]->getClientOriginalName();
                foreach ($pic as $k=>$file){
                    if(!empty($file)){
                        $file->move(public_path() . '/uploads/', time() . uniqid() . '-' . $k . '-laptop');
                    }
                }
        }

0 likes
1 reply
RachidLaasri's avatar
Level 41

Rename files before uploading/saving to the database that way you will have the same name in the folder and your database.

Please or to participate in this conversation.