Laravel 5.2 + 上传文件并在数据库中保存名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37109411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:45:05  来源:igfitidea点击:

Laravel 5.2 + upload file and save name in database

phplaravellaravel-5.1laravel-5.2

提问by Rukmi Patel

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

在 Laravel 5.2 中,我可以使用以下代码上传文件,但找不到将上传的文件名存储在数据库中的方法。

    $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?

有谁知道如何在db中存储文件名?

采纳答案by Rukmi Patel

I got that. Silly Mistake!!

我明白了。愚蠢的错误!!

need to replace line from

需要更换线

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

to

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

回答by lewis4u

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($input);
}

回答by ImBhavin95

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.');
}

回答by Ali

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);
    }