【问题标题】:$file->move() function not showing moved file in the new folder?$file->move() 函数没有在新文件夹中显示移动的文件?
【发布时间】:2018-08-24 14:21:12
【问题描述】:

我正在尝试获取用户上传的文件并将其移动到我的 Laravel Public 文件中名为“Images”的文件夹中,我设法让它成功执行且没有错误,我唯一的问题是上传的文件不会显示在 /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

我的 laravel 项目中图片文件夹的位置是:

我做错了什么以及如何解决这个问题?

【问题讨论】:

  • 你去那个目录看看里面有没有文件吗? (不使用您的 IDE/编辑器)
  • 是的,我使用了我的终端,没有任何显示。

标签: php laravel


【解决方案1】:

使用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();

【讨论】:

  • 嗨伙计,我现在收到了 "Could not move the file "/tmp/phpfGOY2U" to "/home/vagrant/code/public/Images/locationOfFile.png" (move_uploaded_file(): Unable to move '/tmp/phpfGOY2U' to '/home/vagrant/code/public/Images/locationOfFile.png') 的原始错误,我的朋友建议我改用 StoreAs()
猜你喜欢
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多