dongzhi1851 2015-02-18 17:31
浏览 83
已采纳

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

I have a form that uploads an image file to a post, very simply and it uses image intervention

"intervention/image": "dev-master"

so I can resize it etc but at the moment its a simple post action like so:

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


    }

The post checks for validations and if all are correct it sends the post to save it and should hopefully generate the file. When it saves it the file goes into my /public/img/posts/ folder fine. here is the post model below;

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

}

So you can see what I am trying to output here is what I am using in the post.index page to output the image url here:

{!! 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() !!}

When I go to get the file url to output I get this instead:

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

This is what I get, is there something wrong with this and why is it rendering this only?

  • 写回答

1条回答 默认 最新

  • dph87312 2015-02-18 18:26
    关注

    When uploading a file in Laravel, accessing it via the Input::file method will return an instance of Symfony\Component\HttpFoundation\File\UploadedFile, so assigning 'featured_image' => Input::file('featured_image') would not work.

    So instead of building the $entry details array before you save the file to disk, you should actually generate the image path and filename, and store that in the database. Also, unless you want to resize or manipulate the image in any way, there's no need to use the Intervention library. This should do just fine:

    // 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
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?