代码之家  ›  专栏  ›  技术社区  ›  Idhamsyah

无法在Laravel 5.2中上载图像

  •  2
  • Idhamsyah  · 技术社区  · 7 年前

    $name   = $request->input('name');
    $images = $request->file('image');
    $name_img = $images->getClientOriginalName();
    $ext = $images->getClientOriginalExtension();
    // Move Uploaded File
    $destinationPath = 'img/';
    $images->move($destinationPath,$name_img);
    $path = $destinationPath.'/'.$name_img;
    

    然后将它们放入数组,然后插入数据库

    $data = array(
    'name' => $name,
    'image' => $path, );
    DB::table('daftar')->insert($data);
    return json_encode(array('status' =>true));
    

    该代码在Laravel 5.1中有效,但在Laravel 5.2中不起作用。

    Laravel 5.2的代码有问题吗?

    谢谢。:)

    1 回复  |  直到 7 年前
        1
  •  0
  •   Majid Abbasi    7 年前

    我正在使用这种方法,并且它是有效的:

    //Use should have use App\Daftar in above your class;
    //Your method inputs should be like UploadRequest $request , Daftar $daftar
    $file=$request->file('image');
    $name=time().'-'.$file->getClientOriginalName();
    $path = public_path().'/img';
    $file->move($path, $name);
    
    
    $daftar->create(array('name' => $name, 'image' => $path));
    

    确保表中的字段不能为空

    https://laravel.com/docs/5.5/helpers#method-public-path

    我的上传文件夹也在公共目录中

    如果要确保保存在数据库中的数据与发送支票的数据相同,请使用以下方法:

    //Use should have use App\Daftar in above your class;
    //Your method inputs should be like UploadRequest $request , Daftar $daftar
    $file=$request->file('image');
    $name=time().'-'.$file->getClientOriginalName();
    $path = public_path().'/img';
    $file->move($path, $name);
    
    print 'name is'.$name.' and image is '.$path;
    
    $daftar->create(array('name' => $name, 'image' => $path));
    

    然后检查保存在数据库中的数据,两个数据应该相同。