为什么 laravel 图片上传在视图中显示不正确 url?
Why does laravel image upload show incorrect url in view?
我有一个将图像文件上传到 post 的表单,非常简单,它使用图像干预
"intervention/image": "dev-master"
所以我可以调整它的大小等等,但目前它是一个简单的 post 动作,如下所示:
<?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 模型;
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
方法访问它会 return Symfony\Component\HttpFoundation\File\UploadedFile
的实例,因此分配 'featured_image' => 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
));
我有一个将图像文件上传到 post 的表单,非常简单,它使用图像干预
"intervention/image": "dev-master"
所以我可以调整它的大小等等,但目前它是一个简单的 post 动作,如下所示:
<?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 模型;
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
方法访问它会 return Symfony\Component\HttpFoundation\File\UploadedFile
的实例,因此分配 'featured_image' => 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
));