【问题标题】:How to upload multiple files at same time in CakePHP 2如何在 CakePHP 2 中同时上传多个文件
【发布时间】:2014-04-09 05:08:43
【问题描述】:

我有一个在 CakePHP 2 中上传图片的功能。它工作正常,但只能同时上传 1 个文件。如何同时上传多张图片。对不起我的英语!

控制器

  public function add() {
        $this->set('title_for_layout', 'File');
        $this->loadModel('File');

        if(!empty($this->data)){
        $data = $this->data;

        $file = $data['File']['filename'];


        if($file['name'] != null){
            $data = $this->data;   
            $file =  $data['File']['filename'];
            move_uploaded_file($file['tmp_name'], WWW_ROOT . 'uploads/' . $file['name']);
            $data['File']['title'] = $this->data['File']['title'];
            $data['File']['description'] = $this->data['File']['description'];
            $data['File']['filename'] = $file['name'];
            $this->File->save($data,false);
            $this->Session->setFlash(__('Added.', true));
            $this->redirect(array('action'=>'file'));
        }else{

            $this->Session->setFlash(__('Wrong', true));
            $this->redirect(array('action'=>'file'));
        }
        }
}

查看

<?php echo $this->Form->input('filename', array('type' => 'file')); ?>

谢谢。

【问题讨论】:

标签: cakephp cakephp-2.4


【解决方案1】:

抱歉,回答延迟。

基本结构如下:

查看:

<?php echo $this->Form->create('Model',  array('type'=>'file')); ?>
<?php echo $this->Form->input('image.0', array('type' => 'file')); ?>
<?php echo $this->Form->input('image.1', array('type' => 'file')); ?>
<?php echo $this->Form->input('image.2', array('type' => 'file')); ?>
<?php echo $this->Form->input('image.3', array('type' => 'file')); ?>
<?php echo $this->Form->end('Save All');

控制器:

function index()
{    
   if($this->request->is('post', 'put'))
   {
       $this->Model->saveAll($this->request->data['image']);
   }
}

型号:

function beforeSave()
{
    foreach($this->data as $k, $v)
    {
        //code here
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

    echo $this->Form->input('files.', array('type' => 'file', 'multiple'));
    

    然后您可以遍历文件数组并在控制器函数中照常处理它们。 检查CakeBakery.

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多