简体   繁体   English

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

[英]call to a member function getClientOriginalName() on null using laravel

I keep on getting the following error when I try to upload images.当我尝试上传图像时,我不断收到以下错误。 Does anybody have any idea how to solve this?有谁知道如何解决这个问题? I'm confused about what to do next.我对接下来要做什么感到困惑。

Call to a member function getClientOriginalName() on null在 null 上调用成员函数 getClientOriginalName()

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

Controller控制器

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

From what I can tell the $request->file('select_file_Two');据我所知$request->file('select_file_Two'); returns null ;返回null This means most likely that no files was selected for the input with name select_file_Two , make sure the names is the same casing and not select_file_two .这意味着很可能没有为名称为select_file_Two的输入选择文件,请确保名称的大小写相同,而不是select_file_two If all files are required you can add a required to the validator like so如果需要所有文件,您可以像这样向验证器添加required文件

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

or if the are optional you could check if the file exist before hand like so或者如果是可选的,您可以像这样事先检查文件是否存在

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

and skill all the files that were not send.并处理所有未发送的文件。

Also something to note here is you are using rand() to generate the the new name this may in time overwrite files.这里还需要注意的是,您正在使用 rand() 生成新名称,这可能会及时覆盖文件。 I would suggest adding a timestamp like so我建议像这样添加一个时间戳

use Carbon\Carbon;
$name = rand() . '_' . now()->timestamp . '.' . $image->getClientOriginalExtension();

if your codes are ok , which seems so, most of the time is you html form.如果你的代码没问题,看起来是这样,大部分时间都是你的 html 表单。 take a look at your form and see if enctype="multipart/form-data" is included in your form tag, otherwise the files wont be sent.the hole form part should be like this :看看你的表单,看看你的表单标签中是否包含enctype="multipart/form-data" ,否则文件不会被发送。孔表单部分应该是这样的:

    <form method="post" enctype="multipart/form-data" action="URL">
    </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 null laravel 上调用成员函数 getClientOriginalName() 并使用 jquery - Call to a member function getClientOriginalName() on null laravel and using jquery 在 null 上调用成员 function getClientOriginalName() 时出错 - Laravel 8 - Error Call to a member function getClientOriginalName() on null - Laravel 8 在 null laravel 上调用成员函数 getClientOriginalName() - Call to a member function getClientOriginalName() on null laravel 在null上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on null php-在null上调用成员函数getClientOriginalName()并删除警报laravel - php - Call to a member function getClientOriginalName() on null and remove alert laravel Laravel:更新数据时调用null上的成员function getClientOriginalName() - Laravel : Call to a member function getClientOriginalName() on null when Update Data 在数组laravel上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on array laravel laravel 上传文件 - 调用成员函数 getClientOriginalName - laravel upload file - Call to a member function getClientOriginalName 在数组Laravel 5.2上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on array Laravel 5.2 在 laravel 中的 boolean 上调用成员 function getClientOriginalName() - Call to a member function getClientOriginalName() on boolean in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM