繁体   English   中英

如何在CAKEPHP中上传批量记录

[英]How to upload bulk records in CAKEPHP

具有10行的4列仪表板是

提供了用户可以输入的位置

详细信息如下:

Column 1 – File Category
Column 2 – File Type
Column 3 - File Reference
Column 4 – Upload FIle (any type)

上载按钮–上载同时输入的所有文档的详细信息

我无法上载数据库中的所有记录,只有最后一个记录保存在数据库中,请帮我

UserControlle.php(我的控制器)

<?php

App::uses('AppController', 'Controller');

class FilesController extends AppController
{
    public $helpers=array('Html','Form');

    public $components = array('RequestHandler');   

    /*function to display all files details*/
    public function index() 
    {
        $this->set('Files', $this->File->find('all'));
    }

    /*function to add file record into the database */
    public function add() 
    {
        if ($this->request->is('post')) 
        {
            $this->File->create();
            if(empty($this->data['File']['url']['name']))
            {
                unset($this->request->data['url']['file']);
            }
            if(!empty($this->data['File']['url']['name']))
            {
                $file=$this->data['File']['url'];
                $file['name']=$this->sanitize($file['name']);
                $this->request->data['File']['url'] = time().$file['name'];

                if($this->File->save($this->request->data))
                {
                    move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']); 
                    return $this->redirect(array('action' => 'index')); 
                    $this->Session->setFlash(__('Your File has been saved.'));
                    //return $this->redirect(array('action' => 'index'));
                }
            }
            $this->Session->setFlash(__('Unable to add your File.'));
        }
    }

} 

add.ctp(我的看法)

<h1>Add File</h1>
<?php
echo $this->Form->create('File',array('enctype'=>'multipart/form-data'));

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

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

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

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

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

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

echo $this->Form->end('Save File');

这批量如何节省任何东西? 看来您要将上传的文件保存到数据库。 命名模型File也不是一个好主意,因为它与可能导致大量问题的核心类冲突。

通常,如果要批量插入,则需要从CSV中读取行,将它们循环并在每行上调用create()save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM