使用codeigniter多个上传图像

时间:2015-03-11 08:02:16

标签: php codeigniter

嗨我有这个文件上传的图像,我想用多个图像。我一直在谷歌搜索,我发现任何运气都没有。我有这个代码<input type="file" name="photo[]" multiple="" />并且在我的控制器中

        echo ("<pre>");
        print_r($_FILES);
        echo ("</pre>s");
        $name_array = array();
        $count = count($_FILES['photo']['size']);
        $ctr = 0;

        if ($_FILES){
        $files = $_FILES['photo'];
        foreach($_FILES['photo']['name'] as $key=>$value){

            $_FILES['photo']['name']= $files['name'][$ctr];
            $_FILES['photo']['type']    = $files['type'][$ctr];
            $_FILES['photo']['tmp_name'] = $files['tmp_name'][$ctr];
            $_FILES['photo']['error']       = $files['error'][$ctr];
            $_FILES['photo']['size']    = $files['size'][$ctr];  


             echo ("<pre> NEW >>> " . $ctr);
        print_r($_FILES);
        echo ("</pre>");
        $ctr++;
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2048';
                //$config['max_width']  = '1024';
                //$config['max_height']  = '768';
            $this->load->library('upload', $config);

            $this->upload->do_upload();
            $data = $this->upload->data();

            print_r($data);
            $name_array[] = $data['file_name'];
            $names= implode(',', $name_array);
/*          $this->load->database();
            $db_data = array('id'=> NULL,
                             'name'=> $names);
        $this->db->insert('testtable',$db_data);
*/          print_r($names);
        }   

            }

我的行动<?php echo form_open_multipart('post/add') ?>

以及我得到的错误是这样的

A PHP Error was encountered

Severity: Warning

Message: is_uploaded_file() expects parameter 1 to be string, array given

Filename: libraries/Upload.php

Line Number: 161
You did not select a file to upload.

有人可以帮我解决这个问题吗?如何在codeigniter中进行多次上传?任何帮助都非常感谢。感谢

2 个答案:

答案 0 :(得分:0)

控制器:

class Imageupload extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}
function index()
{
    $this->load->view('imageupload_view', array('error' => ' ' ));
}
function doupload() {
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
    for($s=0; $s<=$count-1; $s++) {
    $_FILES['userfile']['name']=$value['name'][$s];
    $_FILES['userfile']['type']    = $value['type'][$s];
    $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
    $_FILES['userfile']['error']       = $value['error'][$s];
    $_FILES['userfile']['size']    = $value['size'][$s];   
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $data = $this->upload->data();
    $name_array[] = $data['file_name'];
        }
        $names= implode(',', $name_array);
 /*         $this->load->database();
        $db_data = array('id'=> NULL,
                         'name'=> $names);
    $this->db->insert('testtable',$db_data);
 */         print_r($names);
 }
 }

视图:

<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('imageupload/doupload');?>
<input name="userfile[]" id="userfile" type="file" multiple="" />
<input type="submit" value="upload" />
<?php echo form_close() ?>
</body>
</html>

答案 1 :(得分:0)

试试这个:

$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['photo']['name']);
for($i=0; $i<$cpt; $i++)
{

    $_FILES['photo']['name']= $files['photo']['name'][$i];
    $_FILES['photo']['type']= $files['photo']['type'][$i];
    $_FILES['photo']['tmp_name']= $files['photo']['tmp_name'][$i];
    $_FILES['photo']['error']= $files['photo']['error'][$i];
    $_FILES['photo']['size']= $files['photo']['size'][$i];    
    $config['upload_path'] = './uploads/';
        ....
        ....

    $this->upload->initialize($config);
    $this->upload->do_upload();
}
相关问题