1

当我尝试上传图片时,我不断收到以下错误。有谁知道如何解决这个问题?我对下一步该做什么感到困惑。

在 null 上调用成员函数 getClientOriginalName()

https://flareapp.io/share/dmkydl73#F42

控制器

public function singalprojectaction(Request $request)
{
    $validation = validator::make($request->all(), [
        'select_file_one' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Two' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Three' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Four' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Five' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($validation->passes()) {
        $select_project = $request->get('select_project');
        $project_name = $request->get('project_name');
        $Client_name = $request->get('Client_name');
        $Completion_date = $request->get('Completion_date');
        $Duration = $request->get('Duration');
        $Description = $request->get('Description');
        $select_file_one = $request->file('select_file_one');
        $select_file_Two = $request->file('select_file_Two');
        $select_file_Three = $request->file('select_file_Three');
        $select_file_Four = $request->file('select_file_Four');
        $select_file_Five = $request->file('select_file_Five');

        $new_name_one = mt_rand().'.'.$select_file_one->getClientOriginalName();
        $select_file_one->move(public_path('projects'), $new_name_one);
        $new_name_Two = mt_rand().'.'.$select_file_Two->getClientOriginalName();
        $select_file_Two->move(public_path('projects'), $new_name_Two);
        $new_name_Three = mt_rand().'.'.$select_file_Three->getClientOriginalName();
        $select_file_Three->move(public_path('projects'), $new_name_Three);
        $new_name_Four = mt_rand().'.'.$select_file_Four->getClientOriginalName();
        $select_file_Four->move(public_path('projects'), $new_name_Four);
        $new_name_Five = mt_rand().'.'.$select_file_Five->getClientOriginalName();
        $select_file_Five->move(public_path('projects'), $new_name_Five);

        $query = DB::table('single_portfolio')->insert([
            'project_id' => $select_project, 'Project_name' => $project_name,
            'Client_Name' => $Client_name, 'Completion_date' => $Completion_date
            , 'Duration' => $Duration, 'Description' => $Description, 'image_one' => $new_name_one, 'image_two' =>
                $new_name_Two,
            'image_three' => $new_name_Three, 'image_four' => $new_name_Four, 'image_five' => $new_name_Five
        ]);

        if ($query) {
            return redirect()->back()->with('messages', 'Data Is Successfully Inserted');
        }

    }

    return redirect()->back();
}
4

2 回答 2

2

据我所知,$request->file('select_file_Two');回报null;这意味着很可能没有为带有名称的输入选择任何文件select_file_Two,请确保名称是相同的大小写而不是相同的大小写select_file_two。如果需要所有文件,您可以required像这样向验证器添加

 `require|image|mimes:jpeg,png,jpg,gif,svg|max:2048`

或者如果是可选的,您可以像这样检查文件是否存在

if(!is_null($select_file_one)){
  $new_name_one = rand() . '.' . $select_file_one->getClientOriginalName();
  $select_file_one->move(public_path('projects'), $new_name_one);
}

并对所有未发送的文件进行技能处理。

另外需要注意的是,您正在使用 rand() 生成新名称,这可能会及时覆盖文件。我建议像这样添加时间戳

use Carbon\Carbon;
$name = rand() . '_' . now()->timestamp . '.' . $image->getClientOriginalExtension();
于 2019-12-24T22:20:43.823 回答
1

如果你的代码是好的,看起来是这样,大多数时候你是 html 表单。看看你的表单,看看enctype="multipart/form-data"你的表单标签中是否包含,否则文件将不会被发送。孔表单部分应该是这样的:

    <form method="post" enctype="multipart/form-data" action="URL">
    </form>
于 2019-12-25T05:01:15.023 回答