如何制作dd($ request-> hasFile('photo'));返回TRUE并在Laravel 5.4中上传图像

时间:2019-07-12 03:19:49

标签: php laravel image image-uploading

我想上传图片,但是我的图片无法上传,我用dd($request->hasFile('supply_photo'));进行检查,得到返回FALSE,然后我用dd($request->all())进行了检查,得到

"_token" => "ETFJYrRIe6MWqk0LAwXXfBBIHEldHBP4bDeICbpi" 
"item" => "1" 
"tgl_date" => "2019-07-11" 
"use_date" => "2019-07-12" 
"details" => "1" 
"photo" => "A.jpg" ]

我有控制器

if($request->hasFile('photo') == true){
                $photo = Validator::make($request->all(), [
                    'photo' => 'image|mimes:jpeg,png,jpg,gif,svg',
                ]);

    if($photo->fails()){
                return redirect()->back()->with('warning', 'Image size should be 2MB or less');
        }            
        $image = $request->file('photo');
        $image_name = rand().'.'. $image->getClientOriginalExtension();
        $destination_path = public_path('/item');
        $image->move($destination_path, $image_name);
    }
        $data->item = $request->item;
        $data->details = $request->details;
        $data->tgl_date = $request->tgl_date;
        //$leave->leave_days = $request->days;
        $data->use_date = $request->use_date;
        $data->id = $id;
        $data->photo = $image_name;
        $data->save();

我的观点

 <label for="photo">Photo</label>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="form-control" name="photo"> 

1 个答案:

答案 0 :(得分:-1)

在laravel应用程序中必须使用laravel存储尝试访问https://laravel.com/docs/5.8/filesystem

在上传文件中是示例代码

use Illuminate\Support\Facades\Storage;
Storage::put('file.jpg', $contents);