Laravel 5.2 + upload file and save name in database

12,472

Solution 1

I got that. Silly Mistake!!

need to replace line from

$input['profile_pic']->pathname = $destinationPath.$file->getClientOriginalName();

to

$input['profile_pic'] = $destinationPath.$file->getClientOriginalName();

Solution 2

Code for uploading files/images and writing real names into database

public function store(Request $request)
{
    $data = $request->all();

    if($file = $request->file('your_file')){

        $name = $file->getClientOriginalName();
        $file->move('folder_where_to_save', $name);
        $data['your_file'] = $name;

    }

    Model_name::create($data); // where $data can be $request->all() or you can manually assign the fields you need
}

Solution 3

You can try this, It wiil help you:

$destinationPath = "test/";
    $file = $request->file('profile_pic');
    if($file->isValid()){
        $file->move($destinationPath, $file->getClientOriginalName());
        $user = User::findOrFail(Auth::user()->id);
        $input = $request->all();
        $input['profile_pic']->pathname = $destinationPath.$file->getClientOriginalName();
        $user->update($request->all()); // Remove This

        // Add this lines

        $data['YOUR_DB_FIELD_NAME'] = $file->getClientOriginalName();
        $user->update($data);
    }

Solution 4

Check this full code

$destinationPath = 'uploads';
$extension = Input::file('prd_img')->getClientOriginalExtension(); 
$fileName = rand(11111,99999).'.'.$extension;
Input::file('prd_img')->move($destinationPath, $fileName);
$data = array(
    'prd_name' => $prd_name,
    'prd_cat' => $prd_cat,
    'prd_sub_cat' => $prd_sub_cat,
    'prd_img' => $fileName,
    'remember_token' => $remember_token,
    'created_at' => $time,
);
if(DB::table('products')->insert($data)){
    return redirect('add-product')->with('success', 'Product Succssfully Added.');
}else{
    return redirect('add-product')->with('error', 'Something wrong please try again.');
}
Share:
12,472
Rukmi Patel
Author by

Rukmi Patel

Thank you for visiting my profile! I am a detailed and thorough professional with over 6 years of Open Source experience. I specialize in delivering quality services with respect for strict deadlines and high expectations. I provide creative and detailed services for web development, Responsive web design and Quality Assurance. I excel at tight deadlines with strict expectations. I possess the self discipline and time management skills necessary to have serverd as employed for the past two years. I can bring value to your business and help provide you innovative solutions in website design and development. Expertise: Laravel, CodeIgniter, Wordpress, PHP, Angular JS, Node JS, Responsive Web Design, Bootstrap Contact: Skype: rukmi.patel Mail: [email protected]

Updated on July 18, 2022

Comments

  • Rukmi Patel
    Rukmi Patel almost 2 years

    In laravel 5.2, I could upload file using below code but could not find a way to store uploaded filename in database.

        $destinationPath = "test/";
        $file = $request->file('profile_pic');
        if($file->isValid()){
            $file->move($destinationPath, $file->getClientOriginalName());
            $user = User::findOrFail(Auth::user()->id);
            $input = $request->all();
            $input['profile_pic']->pathname = $destinationPath.$file->getClientOriginalName();
            $user->update($request->all());
        }
    

    Does anyone know how to store filename in db?

    • Pardeep Poria
      Pardeep Poria almost 8 years
      $filename = $file->getClientOriginalName();This is how to get the name of file and this is how you merge it to inputs Input::merge(array('file_name' => $filename ));