PHP代码用数据库上传多个文件

时间:2016-04-28 08:50:30

标签: php cakephp

我在cakephp中遇到多个文件上传问题。

我尝试上传多个文件,需要在表格中插入多个条目,但我无法做到这一点。

对于Ex - 如果我从表单上传3张照片,则需要在表格中插入3行文件名。

    public function add() {
            $this->Driver->create();
            if ($this->request->is('post')) {
                    for($i=1;$i<4;$i++)
                    {
                        if(empty($this->data['Driver']['document'.$i]['name'])){
                            unset($this->request->data['Driver']['document'.$i]);
                        }

                        if(!empty($this->data['Driver']['document'.$i]['name']))
                        {
                            $file=$this->data['Driver']['document'.$i];
                            $ary_ext=array('jpg','jpeg','xls','docx'); //array of allowed extensions
                            $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                            if(in_array($ext, $ary_ext))
                            {
                                move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']); 
                                $this->request->data['Driver']['document'.$i] = time().$file['name'];
                            }
                        }

                    }

                if ($this->Driver->save($this->request->data)) 
                {
                    //echo "<pre>";print_r($this->request->data); exit();
                    $this->Session->setFlash('Your post has been saved.');
                    $this->redirect(array('action' => 'add'));
                }
                else 
                {
                    $this->Session->setFlash('Unable to add your post.');
                }
            }
        }

add.ctp


    <h1>Add Post</h1><?php
    echo $this->Form->create('Driver', array('url' => array('action' => 'add'), 'enctype' => 'multipart/form-data'));
    echo $this->Form->input('address',array('div' => false, 'class' => 'form-control user-name'));
    for($i=1; $i<4; $i++)
    {
    ?>


<div  id="attachment<?php echo $i;?>" <?php if($i !=1) echo "style='display:none;'";?> >
            <div>
             <?php echo $this->Form->input('document'.$i,array('type'=>'file','label' => false,'div' => false));?>

            </div>
            <div  id="attachmentlink<?php echo $i;?>"  <?php if($i==3) echo "style='display:none;'";?>><a href="javascript:void(0);" onclick="show('attachment<?php echo $i+1;?>'); hide('attachmentlink<?php echo $i;?>');">Add Another Attachment</a></div>
            </div>
            <?php } ?>

<?php
echo $this->Form->end('Save');
?>

2 个答案:

答案 0 :(得分:0)

Plz试试这个

public function add() {
    $this->Driver->create();
    if ($this->request->is('post')) {
            for($i=1;$i<4;$i++)
            {
                if(empty($this->data['Driver']['document'][$i]['name'])){
                    unset($this->request->data['Driver']['document'.$i]);
                }

                if(!empty($this->data['Driver']['document'][$i]['name']))
                {
                    $file=$this->data['Driver']['document'][$i];
                    $ary_ext=array('jpg','jpeg','xls','docx'); //array of allowed extensions
                    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                    if(in_array($ext, $ary_ext))
                    {
                        move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']); 
                        $this->request->data['Driver']['document'][$i] = time().$file['name'];
                    }
                }

            }

        if ($this->Driver->save($this->request->data)){
            $this->Session->setFlash('Your post has been saved.');
            $this->redirect(array('action' => 'add'));
        }else{
            $this->Session->setFlash('Unable to add your post.');
        }
    }
}

答案 1 :(得分:0)

CakePHP 2 Field naming conventions

echo $this->Form->input('Modelname.0.fieldname');
echo $this->Form->input('Modelname.1.fieldname');
视图文件中的

//for($i=1; $i<4; $i++)..
echo $this->Form->input('Driver.'.$i.'.document', array('type' => 'file'));
您的控制器中的

$this->request->data['Driver'][$i]['document']['name']  // where name is uploaded document filename

使用Model::saveMany(array $data = null, array $options = array())

  

用于一次保存同一模型的多行的方法......

if ($this->Driver->saveMany($this->request->data))...

更新您的代码

<?php

public function add()
{
    if ($this->request->is('post')) {
        for($i=1;$i<4;$i++)
        {
            if(empty($this->request->data['Driver'][$i]['document']['name'])){
                unset($this->request->data['Driver'][$i]['document']);
            }

            if(!empty($this->request->data['Driver'][$i]['document']['name']))
            {
                $time = time(); // <-------------
                $file=$this->request->data['Driver'][$i]['document'];
                $ary_ext=array('jpg','jpeg','xls','docx'); //array of allowed extensions
                $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                if(in_array($ext, $ary_ext))
                {
                    move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. $time.$file['name']); 
                    $this->request->data['Driver'][$i]['document'] = $time.$file['name'];
                }
            }

        }
        $this->Driver->create();
        if ($this->Driver->saveMany($this->request->data)) 
        {
            //echo "<pre>";print_r($this->request->data); exit();
            $this->Session->setFlash('Your post has been saved.');
            $this->redirect(array('action' => 'index'));
        }
        else 
        {
            $this->Session->setFlash('Unable to add your post.');
        }
    }
}

<强> add.ctp

<h1>Add Post</h1>
<?php
    echo $this->Form->create('Driver', array( 'type' => 'file'));
    //echo $this->Form->input('address',array('div' => false, 'class' => 'form-control user-name')); ????
    for($i=1; $i<4; $i++)
    {
    ?>


<div  id="attachment<?php echo $i;?>" <?php if($i !=1) echo "style='display:none;'";?> >
            <div>
             <?php echo $this->Form->input('Driver.'.$i.'.document',array('type'=>'file','label' => false,'div' => false));?>

            </div>
            <div  id="attachmentlink<?php echo $i;?>"  <?php if($i==3) echo "style='display:none;'";?>><a href="javascript:void(0);" onclick="show('attachment<?php echo $i+1;?>'); hide('attachmentlink<?php echo $i;?>');">Add Another Attachment</a></div>
            </div>
            <?php } ?>

<?php
echo $this->Form->end('Save');
?>