为什么图片值不从laravel格式上传到数据库?

时间:2018-10-25 03:40:52

标签: laravel-5 image-uploading

我有一个添加新个人资料的表格。在该表单中,有一个文件输入字段,使用户可以将要上传的图片作为文件的路径名上传到DB中。

但是,我的测试表明它未检测到输入字段具有图像,并且没有进入if($ request-> hasFile('image'))语句循环。这是文件上传的表单字段。

 <div class="form-group">
            <div class="row">
              <label for="gambar" class="col-md-3 control-label">Gambar (Saiz gambar 250x300 pixels)</label>
              <div class="col-md-7">
                {!! Form::file('image', array('class' => 'form-control')) !!}
              </div>
            </div>
        </div>

这是上传图像的控制器功能:

    // **************************
    if ($request->hasFile('image')) {
        $image = $request->input('image');
        $photo = $request->file('image')->getClientOriginalExtension();
        $destination = public_path() . '/images/';
        $request->file('image')->move($destination, $photo);
        $data = ['name' => $request->nama,
             'No' => $request->no_id,
             'nokp' => $request->no_kp,
             'dd' => $dd,
             'yy' => $request->yy,
             'SectionID' => $request->sections,
             'CategoryID' => $request->kategori,
             'OperasiID' => $request->pangkat,
             'AgamaID' => $request->agama,
             'JantinaID' => $request->jantina,
             'BangsaID' => $request->bangsa,
             'nolahir' => $request->no_surat_lahir,
             'kumdarah' => $request->kumdarah,
             'Picture' => $request->$photo,
             ];

        $itemregistrations = DB::table('itemregistrations')->insert($data);

        if($itemregistrations)
            return redirect('profil');
        else
            return redirect()->back()->withInput();

    } else {
    $data = ['name' => $request->nama,
             'No' => $request->no_id,
             'nokp' => $request->no_kp,
             'dd' => $dd,
             'yy' => $requestyy,
             'SectionID' => $request->sections,
             'CategoryID' => $request->kategori,
             'OperasiID' => $request->pangkat,
             'AgamaID' => $request->agama,
             'JantinaID' => $request->jantina,
             'BangsaID' => $request->bangsa,
             'nolahir' => $request->no_surat_lahir,
             'kumdarah' => $request->kumdarah,
             // 'Picture' => $request->$filePath,
             ];

    $itemregistrations = DB::table('itemregistrations')->insert($data);

    if($itemregistrations)
        return redirect('profil');
    else
        return redirect()->back()->withInput();

    }

2 个答案:

答案 0 :(得分:1)

在数据数组中,您定位的是'Picture' => $request->$photo,而不是'Picture' => $photo,

另外,您似乎只是拉扩展名,请查看下面要使用的一些命令的列表以及何时使用

  //Display File Name
  $file->getClientOriginalName();

  //Display File Extension
  $file->getClientOriginalExtension();

  //Display File Real Path
  $file->getRealPath();

  //Display File Size
  $file->getSize();

  //Display File Mime Type
  $file->getMimeType();

要对文件进行修饰

$file = $request->file('image');
$file->move($destination, $file->getClientOriginalName());

答案 1 :(得分:1)

Make sure your form have following attribute
enctype="multipart/form-data"
and use following laravel file upload code

       if ($request->hasFile('image')) {
            $is_file = true;
            $file_name = $input['image'] = time() . '.' . $request->image->getClientOriginalExtension();
            $request->image->move(base_path() . '/assets/images/users/', $input['image']);

        }