PHP Laravel框架上传图像表单处理

PHP Laravel框架上传图像表单处理,php,forms,validation,laravel,laravel-4,Php,Forms,Validation,Laravel,Laravel 4,我试图学习如何处理图像表单,将图像上传到数据库,并允许用户在网站上查看图像,这是使用Laravel4完成的。我必须有某种缺陷,因为视图没有任何错误,但是当我选择要上传的图像并点击表单上的“保存”按钮时,除了看起来表单已经刷新之外,什么也没有发生,因为文件已经不存在了 路线 // This is for the get event of the index page Route::get('/', array( 'as' => 'index_page', 'uses' =&g

我试图学习如何处理图像表单,将图像上传到数据库,并允许用户在网站上查看图像,这是使用Laravel4完成的。我必须有某种缺陷,因为视图没有任何错误,但是当我选择要上传的图像并点击表单上的“保存”按钮时,除了看起来表单已经刷新之外,什么也没有发生,因为文件已经不存在了

路线

// This is for the get event of the index page
Route::get('/', array(
    'as' => 'index_page',
    'uses' => 'ImageController@getIndex'
));

// This is for the post event of the index page
Route::post('/', array(
    'as' => 'index_page_post',
    'before' => 'csrf',
    'uses' => 'ImageController@postIndex'
));
ImageController.php

class ImageController extends BaseController {

public function getIndex()
{
    // Let's first load the form view
    return View::make('tpl.index');
}

public function postIndex()
{
    // Let's validate the form first with the rules which are set at the model
    $input = Input::all();
    $rules = Photo::$upload_rules;

    $validation = Validator::make($input, $rules);

    // If the validation fails, we redirect the user to the index page, with errors

    if ($validation->passes()) {
        // If the validation passes, we upload the image to the database and process it
        $image = Input::file('image');

        // This is the original uploaded client name of the image
        $filename = $image->getClientOriginalName();
        // Because Symfony API does not provide filename
        // without extension, we will be using raw PHP here

        $filename = pathinfo($filename, PATHINFO_FILENAME);

        // We should salt and make an url-friendly version of the file
        $fullname = Str::slug(Str::random(8) . $filename) . '.' .
            $image->getClientOriginalExtension();

        // We upload the image first to the upload folder, then
        // get make a thumbnail from the uploaded image
        $upload = $image->move
            (Config::get('image.upload_folder'), $fullname);

        Image::make(Config::get('image.thumb_folder').'/'.$fullname)
            ->resize(Config::get('image.thumb_width'), null, true)
            ->save(Config::get('image.thumb_folder').'/'.$fullname);

        // If the file is now uploaded we show a success message
        // otherwise, we show an error

        if ($upload) {
            // image is now uploaded, we first need to add column to the database
            $insert_id = DB::table('photos')->insertGetId(
                array(
                    'title' => Input::get('title'),
                    'image' => $fullname
                    )
                );
            // Now we redirect to the image's permalink
            return Redirect::to(URL::to('snatch/'.$insert_id))
                ->with('success', 'Your image is uploaded successfully!');
        }

        else {
            // Image cannot be uploaded
            return Redirect::to('/')->withInput()
                ->with('error', 'Sorry, the image could not be uploaded.');
        }
    }
    else {
        return Redirect::to('/')
            ->withInput()
            ->withErrors($validation);
    }
}
图像模型

class Photo extends Eloquent {

// the variable that sets the table name
protected $table = 'photos';

// the variable that sets the table name
protected $fillable = array('title', 'image');

// the timestamps enabled
public $timestamps = true;

// Rules of the image upload form
public static $upload_rules = array(
        'title' => 'required|min:3',
        'image' => 'required|image'
    );
}

窗体的视图

@extends('frontend_master')

@section('content')

{{ Form::open(array('url' => '/', 'files' => true )) }}

{{ Form::text('title', '', array(
    'placeholder' => 'Please insert your title here')) }}

{{ Form::file('image') }}

{{ Form::submit('save', array('name' => 'send')) }}

{{ Form::close() }}

@stop
如果你能找到任何bug,请告诉我,我敢肯定我的电脑一定出了问题ImageController@postIndex


感谢您提供的任何见解

2您需要查看的内容

首先,更新composer.json以包含干预/图像包之后。您应该运行
composer dump autoload
来刷新自动加载文件

第二,你的控制器有一个逻辑错误

Image::make(Config::get('image.thumb_folder').'/'.$fullname)
    ->resize(Config::get('image.thumb_width'), null, true)
    ->save(Config::get('image.thumb_folder').'/'.$fullname);
应该是

Image::make(Config::get('image.image_folder').'/'.$fullname)
    ->resize(Config::get('image.thumb_width'), null, true)
    ->save(Config::get('image.thumb_folder').'/'.$fullname);
因为您已使用以下代码将图像文件移动到image_文件夹:

$upload = $image->move
    (Config::get('image.upload_folder'), $fullname);
希望这有帮助