【问题标题】:Why does laravel image upload show incorrect url in view?为什么 laravel 图片上传在视图中显示不正确的 url?
【发布时间】:2015-04-19 19:57:49
【问题描述】:

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

"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 页面中用来在此处输出图像 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" =&gt; "/private/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/phpMEtuuA"

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

【问题讨论】:

  • [laravel.com/docs/4.2/requests#files]file方法返回的对象是Symfony\Component\HttpFoundation\File\UploadedFile类的实例,扩展了PHP的SplFileInfo类,提供了多种交互方法文件。

标签: php post laravel upload


【解决方案1】:

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

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

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

【讨论】:

  • 只是一个快速的,我怎样才能获得文件末尾的扩展名,例如.png?我试过$type = pathinfo($img, PATHINFO_BASENAME); 然后完成$filename = date('Y-m-d-H:i:s') . $extension . $type; 但它仍然返回空白
  • 我的回答有误。要提取扩展名,代码应该是$extension = pathinfo($image-&gt;getClientOriginalName(), PATHINFO_EXTENSION);。此外,文件名应手动包含.,因为pathinfo 返回不带点的扩展名。我已经更新了答案中的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2011-02-09
  • 2014-10-22
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多