【问题标题】:Trying to upload a file in laravel but it doesn't seem to exist尝试在 laravel 中上传文件,但它似乎不存在
【发布时间】:2019-05-12 12:56:34
【问题描述】:

您好,我是 laravel 新手,正在尝试上传 .csv 文件。

我的代码如下所示:

CLIENT_SIDE(刀片)

 <form role='form' action="{{route('webscrape.upload')}}" method ="post" enctype="multipart/form-data" data-parsley-validate="">

   {{ csrf_field() }}

   <input type='file' name="data" required="">
   <br>
   <input type='submit' name='submit' value='Import' class="btn btn-info btn-lg" onclick="return confirm('Are you sure you want to submit these information?')">
 </form>

SERVER_SIDE(控制器)

public function uploadFile(Request $request){

    if ($request->input('submit') != null ){
       $file = $request->file('data');
       if (file_exists('data')) {
         dd('hello');
       } else {
        dd('hi');
      }
    }
}

即使我上传文件,我也会一直打招呼。我已经检查了路线以确保名称和链接都很好。任何建议都会很棒。谢谢你

【问题讨论】:

    标签: php html laravel-5


    【解决方案1】:

    据我所知,您使用file_exists() 来检查字符串而不是实际的文件路径,在这种情况下它将永远无法工作。

    我给你的建议:

    您应该传递要检查的文件的实际路径。为此,您可以编写如下内容:

    $file = $request->file('data');
    $path = $file->getPathname();
    if (file_exists($path)) {
       echo 'hello';
    } else {
       echo 'hi';
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 2016-08-23
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2016-05-31
      相关资源
      最近更新 更多