Laravel $ file-> move()没有在新文件夹中显示移动的文件?

时间:2018-08-24 14:21:12

标签: laravel

我正在尝试获取用户上传的文件,并将其移动到我的Laravel Public文件内部的名为“ Images”的文件夹中,我设法使其成功“执行”,没有错误,这是我唯一的问题是上传的文件不会显示在“图片”文件夹中,我的功能如下:

public function contactPost(Request $request)
 {

    if($request->hasFile('attachment'))
    {
        $file = $request->file('attachment');
        $file->move('Users/jvb/laravel/public/Images', $file->getClientOriginalName());
        echo 'File name '.$file->getClientOriginalName();
        echo "<br>";
    } else {
        return "No";
    }
 }

blade.php文件如下:

@extends('layouts.master')


 @section('content')
<form method="POST" enctype="multipart/form-data" action="{{ route('contactPost') }}">

    @csrf

    <div class="form-group">
        <input class="form-control" name='name' type="text" placeholder="Name">    
    </div>

    <div class="form-group">
        <input class="form-control" name='email' type="email" placeholder="Email">    
    </div>

    <div class="form-group">
        <textarea class="form-control" name="message" id="" cols="30" rows="10" placeholder="Message"></textarea>
    </div>

     <div class="form-group">
        <input class="form-control" name="attachment" type="file">    
    </div>

    <div class="form-group">
        <button type="submit" class="form-control">Send Message</button>
    </div>

</form>
 @endsection

图片文件夹的位置:

location of Image folder

1 个答案:

答案 0 :(得分:2)

使用public_path()代替绝对路径

 $file->move(public_path()."/Images/", $file->getClientOriginalName());

更新  为了将图像存储到数据库中

 $user = new User; //model
  $filename = $file->getClientOriginalName();
  $file->move(public_path("Images"), $filename);
  $path = '/Images/' . $filename;
  $user->avatar = $path;
  $user->save();