为什么laravel图片上传在视图中显示不正确的url


Why does laravel image upload show incorrect url in view?

我有一个表单,可以非常简单地将图像文件上传到帖子中,它使用图像干预

"intervention/image": "dev-master"

所以我可以调整它的大小等,但目前这是一个简单的后期操作,比如:

<?php namespace Boroughcc'Http'Controllers;
use Input;
use Redirect;
use Storage;
use SirTrevorJs;
use STConverter;
use Validator;
use Image;
use Boroughcc'Post;
use Boroughcc'Http'Requests;
use Boroughcc'Http'Controllers'Controller;
use Illuminate'Http'Request;
class PostsController extends Controller {
public function store()
    {
        // image upload function
        // $img = Image::make(Input::file('featured_image'));
        // dd($img);
        $input = Input::all();
        $validation = Validator::make($input, Post::$rules);
        $entry = array(
            'title' => Input::get('title'),
            'featured_image' => Input::file('featured_image')        
        );
        if ($validation->passes())
        {
            $img = Image::make(Input::file('featured_image'));
            $pathinfo = pathinfo($img);
            $type = $pathinfo['basename'];
            $filename = date('Y-m-d-H:i:s').$type;
            $path = 'img/posts/' . $filename;
            $img->save($path);
            $post = Post::create(
                $entry
            );
            return Redirect::route('posts.index')->with('message', 'Post created');
        } else {
                return Redirect::route('posts.create')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors.');
        }
        //Post::create( $input );
        // return Redirect::route('posts.index')->with('message', 'Post created');

    }

post检查验证,如果所有验证都正确,它会发送post来保存它,并有望生成文件。当它保存它时,文件会很好地进入我的/public/img/posts/文件夹。下面是后模型;

Post.php

<?php namespace Boroughcc;
use Illuminate'Database'Eloquent'Model;
class Post extends Model {
    //
    protected $guarded = [];
    public static $rules = array(
        'title' => 'required',
        'featured_image' => 'required|image|mimes:jpeg,jpg,png,bmp,gif,svg'
    );
}

所以你可以看到我试图在这里输出的是我在post.index页面中使用的图像url:

{!! Form::model(new Boroughcc'Post, ['route' => ['posts.store'], 'files' => true]) !!}
<div class="form-group">
    {!! Form::label('title', 'Title:') !!}
    {!! Form::text('title') !!}
</div>
<div class="form-group">
    <strong>Only edit this if necessary, this is auto populated</strong><br>
    {!! Form::label('slug', 'Slug:') !!}
    {!! Form::text('slug') !!}
</div>
<div class="form-group">
    {!! Form::label('featured_image', 'Featured Image:') !!}
    {!! Form::file('featured_image') !!}
</div>
<div class="form-group">
    {!! Form::label('body', 'Post body:') !!}
    {!! Form::textarea('body', null, array('id'=>'','class'=>'sir-trevor')) !!}
</div>
<div class="form-group">
    {!! Form::submit($submit_text, ['class'=>'btn primary']) !!}
</div>
{!! Form::close() !!}

当我去获取要输出的文件url时,我得到的是:

"featured_image" => "/private/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/phpMEtuuA"

这就是我所得到的,这有什么问题吗?为什么它只渲染这个?

在Laravel中上传文件时,通过Input::file方法访问该文件将返回Symfony'Component'HttpFoundation'File'UploadedFile的实例,因此分配'featured_image' => Input::file('featured_image')将不起作用。

因此,与其在将文件保存到磁盘之前构建$entry细节数组,不如实际生成图像路径和文件名,并将其存储在数据库中。此外,除非您想以任何方式调整图像大小或操作图像,否则无需使用干预库。这应该做得很好:

// Get the uploaded file object
$image = Input::file('featured_image');
// Generate the necessary file details
$extension = pathinfo($image->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = date('Y-m-d-H:i:s') . '.' . $extension;
$path = 'img/posts/';
// Move the uploaded image to the specified path
// using the generated specified filename
$image->move($path, $filename);
// Save the post to the database
// using the path and filename use above
$post = Post::create(array(
    'title' => Input::get('title'),
    'featured_image' => $path . $filename
));