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

时间:2019-12-24 21:41:12

标签: laravel laravel-validation laravel-formrequest

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

  

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

2 个答案:

答案 0 :(得分:2)

据我所知package studenti; import java.util.Scanner; public class Studenti { /** * @param args the command line arguments */ public static void main(String[] args) { // deklarimi i variables, inicializimi behet nga perdoruesi int Vijueshmeria; // deklarimi i variablave te tipit double double detyrat, k1, k2, provimi, projekt, rezultati; // deklarimi dhe inicializmi i variablave string String emri="Abdurrahman"; String mbiemri="Cakolli"; Scanner obj=new Scanner(System.in); System.out.println("Shkruani vijueshmerine (int):"); Vijueshmeria=obj.nextInt(); System.out.println("Shkruani detyrat(double):"); detyrat =obj.nextDouble(); System.out.println("Shkruani detyren e k1(double):"); k1=obj.nextDouble(); System.out.println("Shkruani detyrat e k2(double):"); k2=obj.nextDouble(); System.out.println("Shkruani piket e projektit :"); projekt=obj.nextInt(); provimi=((k1+k2)/2*0.6+detyrat+projekt); rezultati=Vijueshmeria+detyrat+provimi; System.out.println("\n"+emri+""+mbiemri+",ju keni:"+rezultati+"pike"); 返回$request->file('select_file_Two');;这意味着很可能没有为名称为null的输入选择文件,请确保名称使用相同的大小写而不是select_file_Two。 如果需要所有文件,您可以像这样

select_file_two添加到验证器中
required

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

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

并熟练处理所有未发送的文件。

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

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

答案 1 :(得分:1)

如果您的代码正常,这似乎是大多数情况下的HTML表单。 查看您的表单,看看表单标签中是否包含enctype="multipart/form-data",否则文件将不会发送。孔表单部分应如下所示:

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