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

时间:2014-01-12 00:47:17

标签: php forms validation laravel laravel-4

我正在尝试学习一个将图像上传到数据库并允许用户在网站上查看图像的过程图像表单,这是使用Laravel 4完成的。我必须遇到某种错误,因为视图没有有任何错误,但是当我选择要上传的图像并点击我的表单上的“保存”按钮时,除了表单已经刷新因为文件已经消失之外没有任何其他事情发生。

路线

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

如果您能找到任何错误,请告诉我,我很确定在我的ImageController @ postIndex

中一定有问题

感谢您的任何见解

1 个答案:

答案 0 :(得分:0)

你需要检查的两件事。

第一次关闭,一旦您更新了composer.json以包含干预/图像包。您应该运行composer dump-autoload来刷新自动加载文件。

2,您的控制器出现逻辑错误。

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_folder:

$upload = $image->move
    (Config::get('image.upload_folder'), $fullname);

希望这有帮助。

相关问题