codeigniter中的两个多文件上传器

时间:2015-10-31 05:57:48

标签: php codeigniter file-upload

我一直试图在codeigniter项目中以单一形式上传多张照片两次,尽管这些照片正确上传但照片名称并未按照要求返回。例如,如果要上传名为' a.jpg'在第一个上传者和' b.jpg'在第二个上传者中,我得到的名字是" a.jpg,b.jpg"首先和" b.jpg,b(1).jpg"对于第二种情况。以下是附加的代码。

控制器: -

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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']);
    //  echo $count;
        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'] = 'application/views/uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000000';
            //$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);
        return $names;
    }

    function doupload_1()
    {
        $name_array = array();
        $count = count($_FILES['file_1']['size']);
        echo $count;
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++)
        {
            $_FILES['file_1']['name']=$value['name'][$s];
            $_FILES['file_1']['type']    = $value['type'][$s];
            $_FILES['file_1']['error']       = $value['error'][$s];
            $_FILES['file_1']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['file_1']['size']    = $value['size'][$s];
            $config['upload_path'] = 'application/views/uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000000';
            //$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);
        return $names;
    }

    public function check()
    {
        $data = $this->doupload();
        $data_1 = $this->doupload_1();
        print_r($data);
        echo "<br>";
        print_r($data_1);
    }

}
?>

视图: -

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

提前感谢!

3 个答案:

答案 0 :(得分:0)

最后我得到了代码中的问题。这是因为$ _FILES上的foreach不是$ _FILES ['userfile']这就是为什么$ _FILES包含所有文件而不仅仅是userfile。和file_1

foreach($_FILES['userfile'] as $key=>$value)

foreach($_FILES['file_1'] as $key=>$value)

但仍然需要根据foreach更改以下代码。 如果你找到答案的解决方案,请给我答案,并接受它。

答案 1 :(得分:0)

请检查以下链接以在codeigniter中上传多个文件

http://technet.massivetechnolab.co.in/multiple-image-uploading-using-codeigniter

答案 2 :(得分:0)

多重上传功能

public function mupload ($field_name,$path = './botimage/') {

    $files = $_FILES;
    $cpt = count($_FILES[$field_name]['name']);
    for ($i = 0; $i < $cpt; $i++) {
        $config =  array(
            'upload_path'     => $path,
            'upload_url'      => upload_url('galeri/'),
            'allowed_types'   => "gif|jpg|png|jpeg",
            'overwrite'       => TRUE,
            'file_name'       => rand(0,999). time(),
            'max_size'        => "10000KB",
        );
        $data = array();
        // set the filter image types
        $this->load->library('upload', $config);
        $_FILES['userfile']['name'] = $files[$field_name]['name'][$i];
        $_FILES['userfile']['type'] = $files[$field_name]['type'][$i];
        $_FILES['userfile']['tmp_name'] = $files[$field_name]['tmp_name'][$i];
        $_FILES['userfile']['error'] = $files[$field_name]['error'][$i];
        $_FILES['userfile']['size'] = $files[$field_name]['size'][$i];

        $this->upload->initialize($config);
        if ($this->upload->do_upload()) {
            $tmp = $this->upload->data();
            $uploaded_file_data[$i] = $tmp["file_name"];
        } else {
            $errors[] = $this->upload->display_errors();
        }
    }

    $data["file_urls"] = implode(",",$uploaded_file_data);
    $data["errors"] = implode(" ",@$errors);

    return $data;
}