laravel图片上传错误

时间:2017-08-27 16:00:42

标签: php image laravel-5 intervention

我正在使用干预包上传laravel图像,从创建到保存图像一切都很好但是数据库中的图像列中的问题它存储了带路径的名称

/私有的/ var /文件夹/ 18 / 0w78rt691m99y_kv8xln4n0c0000gn / T / phpFkP3Gh

这是我的代码:

if($request->hasFile('image')) {
  $image = $request->file('image');
  $filename = time() . '.' . $image->getClientOriginalExtension();
  $location = public_path('images/' . $filename);
  \Image::make($image)->save($location);
 }

这是存储在图像文件1503847676.jpg

中的图像名称

请帮我找一下,问题出在哪里?

这是我的整个方法

public function update(Request $request, $id)
    {

        // Validating fields requests
        $this->validate($request, [

            'title'     =>  'required|min:6',
            'subtitle'  =>  'required|min:6',
            'slug'      =>  'required',
            'body'      =>  'required',
            'image'     =>  'required|mimes:jpeg,png,jpg,gif,svg'
        ]);

        if($request->hasFile('image')) {
           $image = $request->file('image');
           $filename = time() . '.' . $image->getClientOriginalExtension();
           $location = public_path('images/' . $filename);
           \Image::make($image)->save($location);
        }

        //find target post
        $post = Post::find($id);

        //create upadeted data from inputs fields
        $post->title = $request->title;
        $post->subtitle = $request->subtitle;
        $post->image = $filename;
        $post->slug = $request->slug;
        $post->image = $request->image;
        $post->status = $request->status;
        $post->body = $request->body;

        //save the new data to database
        $post->save();
        $post->tags()->sync($request->tags);
        $post->categories()->sync($request->categories);

        return redirect('/admin/post');


    }

3 个答案:

答案 0 :(得分:0)

您无需Intervention个包即可进行简单的图片上传,无需任何修改,例如调整大小等。试试这个

if( $request->hasFile('image')) {
    $image = $request->file('image');
    $location = public_path(). '/images/';
    $filename = time() . '.' . $image->getClientOriginalExtension();
    $image->move($location, $filename);

}
//find target post
$post = Post::find($id);
$post->image = $filename;

//remaining code
$post->save();

答案 1 :(得分:0)

如果您使用干预图像

,可以尝试此操作
$image = Input::file('image');
$filename  = time() . '.' . $image->getClientOriginalName();
$path = public_path('images/' . $filename);
Image::make($image->getRealPath())->save($path);
$post->image = 'images/' . $filename;

答案 2 :(得分:0)

我遇到了类似你的错误,我使用以下方法解决了这个错误:

$file = Input::file('image');
$image->move($location, $file->getClientOriginalName());