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

问题描述 投票:2回答:4

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

有谁知道如何存储文件名以dB为单位?

php laravel laravel-5.1 laravel-5.2
4个回答
6
投票

代码上传文件/图片和写作的真实姓名为数据库

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

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()); // Remove This

        // Add this lines

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

2
投票

检查这个完整的代码

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

0
投票

我明白了。愚蠢的错误!

需要从更换线

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

$input['profile_pic'] = $destinationPath.$file->getClientOriginalName();
© www.soinside.com 2019 - 2024. All rights reserved.