简体   繁体   English

上传的图像是 Null Laravel,我收到此错误 file_get_contents(): Filename cannot be empty

[英]Uploaded Image Is Null Laravel and I am getting this error file_get_contents(): Filename cannot be empty

I was trying to convert the image uploaded into a form into base 64 in Laravel but the uploaded image is null我试图将上传的图像转换为 Laravel 中的 base 64 但上传的图像是 null

Blade File-->(View)刀片文件-->(查看)

<body>
   
    <form action="{{Route('PostImageProcess')}}" enctype='multipart/form-data'>
        {{ csrf_field() }}
        <div class="form-group">
            {{-- {!! Form::label($for, $text, [$options]) !!} --}}
            <label for="img">post Image</label>
            <input type="file" name="image" id="img">
        </div>
        <input type="submit">
    </form>
</body>

Inside Controller-->内部控制器-->

public function PostImageProcess(Request $request){

    // $image = base64_encode(file_get_contents($request->file('image')));
    

    $ima= base64_encode(file_get_contents($request->file('image')));
    echo $ima;
    
    $image="11221";
    
   
    return view('afterPostSuccess')->with("body",$image);
    
}

and I am getting this error: file_get_contents(): Filename cannot be empty我收到此错误:file_get_contents(): Filename cannot be empty

You are not validating your Request $request您没有验证您的Request $request

        $valid = $request->validate([
                     'image' => 'required|max:100024',
                ]);

         $ima= base64_encode(file_get_contents($request->file('image')));
         echo $ima;

          $image="11221";
          return view('afterPostSuccess')->with("body",$image);

UPDATE更新

Edit your form like:编辑您的表格,如:

  <body>
     <form method="post" action="{{Route('PostImageProcess')}}" enctype='multipart/form-data'>
       {{ csrf_field() }}
      <div class="form-group">
          {{-- {!! Form::label($for, $text, [$options]) !!} --}}
          <label for="img">post Image</label>
          <input type="file" name="image" id="img">
      </div>
      <input type="submit">
    </form>
  </body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM