doupinwan0563 2016-06-09 05:26
浏览 22
已采纳

Laravel 5文件和图片上传

View:

<input type="file" name="photo">
<button class="btn">Send</button>
<input type="submit" value="send">    //    I tested by input and button but it also does not work

Controller:

$f = $request->file("photo");
if($f->isValid()){
  echo "upload"
  $f->move("/public/upload/file.png");
}else{
  echo 'not upload';
}

Error:

Fatal error: Call to a member function isValid() on null

I do not know why but I can not do upload images to the site. I've tried many ways but nothing works. Send files by request and get an error, I tried to use a Request::file('photo') or Input::file('photo')->isValid() but it not work too.

var_dump($request->file('prod_photo'));

If you will use such code in response gets NULL

EDIT:

$f = $request->file('photo');
if ($f->isValid()) {
            $ext = $f->getClientOriginalExtension();
            $org_name = $f->getClientOriginalName();
            $r = rand(11111,999999);
$f->move("public/uploads", $r.$org_name);
    //
}

How can I change the image size and check if the file is certainly the picture?

  • 写回答

1条回答 默认 最新

  • dongnue4923 2016-06-09 05:42
    关注

    Add enctype="multipart/form-data" to your form

    EDIT:

    You can easily check it with validator, like so:

    $this->validate($request, [
        'photo' => 'required|image' //works for jpeg, png, bmp, gif, or svg
    ]);
    

    Unfortunely u cant change image size with native php (as i know), you need to use either ImageMagick, GD or Glide (which needs previous one)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?