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

时间:2015-02-18 17:31:25

标签: php post laravel upload

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

"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');


    }

帖子检查验证,如果一切正确,它会发送帖子保存,并希望生成文件。当它保存时,文件会很好地进入我的/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页面中使用的输出图片网址:

{!! 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"

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

1 个答案:

答案 0 :(得分:0)

在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
));