如何使用类型为'file'和'multiple'的<input />发布表单而不会使请求变黑?

时间:2015-01-05 10:17:40

标签: php cakephp

大家早上好。

我在发布表单时遇到问题:我的目标是使用我的表单上传许多文件,在我的模型中处理上传,但我甚至无法访问Model :: beforeSave(),因为我的请求不断变黑。这是代码。

查看:

echo $this->Form->create('Gallery', array('type' => 'file'));
echo $this->Form->hidden('id');
echo $this->Form->input('title', array(
    'required'  => true,
    'label'     => false,
    'class'     => 'form-control'
));
echo $this->Form->input('description', array(
    'required'  => true,
    'label'     => false,
    'type'      => 'textarea',
    'class'     => 'ckeditor'
));
echo $this->Form->input('photos.', array(
    'required'  => false,
    'label'     => false,
    'type'      => 'file',
    'class'     => 'form-control',
    'multiple'  => 'multiple'
));
echo $this->Form->end(array(
    'label'     => $button,
    'class'     => 'btn btn-default'
));

控制器:

public function admin_update($id = null) {
    if ($id != null && !$this->Gallery->exists($id)) {
        throw new NotFoundException('Elemento non valido');
    }
    if ($this->request->is(array('post', 'put'))) {
        if ($this->Gallery->save($this->request->data)) {
            $this->Session->setFlash('Galleria salvata', 'default', array(), 'good');
            return $this->redirect(array(
                'admin'     => true,
                'action'    => 'index'
            ));
        }
        $this->Session->setFlash('Non sono riuscito a memorizzare la galleria', 'default', array(), 'bad');
    }
    $this->request->data = $this->Gallery->findById($id);
}

型号:

App::uses('AppModel', 'Model');
App::uses('String', 'Utility');
class Gallery extends AppModel {
    public $useTable = 'gallery';
    public $displayField = 'title';
    public $validate = array(
        'title' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty')
            )
        ),
        'description' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty')
            )
        )
    );

    public function afterSave($created, $options = array()) {
        $dir = new Folder('img/galleries/' . $this->id, true, 0777);

        foreach ($this->data[$this->alias]['photos'] as $Photo) {
            move_uploaded_file($Photo['tmp_name'], 'img/galleries/' . $this->id . '/' . String::uuid()); // I'll add more controls later
        }
    }

    public function beforeDelete($cascade = true) {
        $dir = new Folder('img/galleries/' . $this->id);
        return $dir->delete();
    } 
}

因此,每次我尝试上传我的表单时,我的请求都会出现此错误

The request has been black-holed
Error: The requested address '/ciardi/admin/galleries/update' was not found on this server.

Stack Trace
CORE/Cake/Controller/Component/SecurityComponent.php line 239 → SecurityComponent->blackHole(GalleriesController, string)
[internal function] → SecurityComponent->startup(GalleriesController)
CORE/Cake/Utility/ObjectCollection.php line 128 → call_user_func_array(array, array)
[internal function] → ObjectCollection->trigger(CakeEvent)
CORE/Cake/Event/CakeEventManager.php line 242 → call_user_func(array, CakeEvent)
CORE/Cake/Controller/Controller.php line 675 → CakeEventManager->dispatch(CakeEvent)
CORE/Cake/Routing/Dispatcher.php line 187 → Controller->startupProcess()
CORE/Cake/Routing/Dispatcher.php line 165 → Dispatcher->_invoke(GalleriesController, CakeRequest)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)

即使我尝试使用相同的代码上传单个图像,也会出现同样的问题。 有没有办法在没有插件的情况下解决我的问题?请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

解决了我的问题。我只是在我的数据库中添加了一个名为'images'的表和一个Image模型。 Gallery :: afterSave()方法为我正在尝试上传的每个图像调用Image :: save(),Image :: beforeSave()使用is_uploaded_file()检查图像并将其移动到右侧dir和Image :: beforeDelete ()帮助我保持文件系统清洁。就是这样。