简体   繁体   English

试图在laravel中上传文件,但它似乎不存在

[英]Trying to upload a file in laravel but it doesn't seem to exist

Hi I'm new to laravel and was trying to upload a .csv file. 嗨,我是laravel的新手,正在尝试上传.csv文件。

My code looks like this: 我的代码看起来像这样:

CLIENT_SIDE (BLADE) CLIENT_SIDE(BLADE)

 <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 (CONTROLLER) 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');
      }
    }
}

I keep getting hi even when I upload a file. 即使我上传文件,我也一直很喜欢。 I already checked the routes to make sure the names and links are fine. 我已经检查了路线,以确保名称和链接没问题。 Any advice would be great. 任何建议都会很棒。 Thank You 谢谢

From what i perceive, you are using file_exists() to check a string instead of the actual file path and in this case it will never work. 根据我的感知,您使用file_exists()来检查字符串而不是实际的文件路径,在这种情况下它将永远不会工作。

My advice for you: 我给你的建议是:

You should pass the actual path of the file that you want to check for. 您应该传递要检查的文件的实际路径。 In order to do that, you can write something like this: 为了做到这一点,你可以这样写:

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

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

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