Uploading multiple images in codeigniter not working

时间:2017-05-24 09:24:36

标签: php codeigniter

I want to upload my images array in codeigniter. The names of images are name = standimages[]. This is my controller

$config['upload_path'] = './uploads/individual_stands/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['overwrite'] = FALSE;
echo $this->upload_images($config, $_FILES, 'standimages');

And my function

function upload_images($config, $files, $name) {

    if (!file_exists($config['upload_path'])) {
        mkdir($config['upload_path'], 0777, true);
    }

    $filesCount = count($files[$name]['name']);
    for ($i = 0; $i < $filesCount; $i++) {
        $files['userFile']['name'] = $files[$name]['name'][$i];
        $files['userFile']['type'] = $files[$name]['type'][$i];
        $files['userFile']['tmp_name'] = $files[$name]['tmp_name'][$i];
        $files['userFile']['error'] = $files[$name]['error'][$i];
        $files['userFile']['size'] = $files[$name]['size'][$i];

        pre($files);

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ($this->upload->do_upload($files['userFile'])) {
            $fileData = $this->upload->data();
            $uploadData[$i]['file_name'] = $fileData['file_name'];
            $uploadData[$i]['created'] = date("Y-m-d H:i:s");
            $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
        }
    }

    if (!empty($uploadData)) {
        //Insert file information into the database
        $insert = $this->file->insert($uploadData);
        return $statusMsg = $insert ? 'Files uploaded successfully.' : 'Some problem occurred, please try again.';
    }
}

Any idea why my images are not being uploaded?

4 个答案:

答案 0 :(得分:1)

您必须将输入名称发送到do_upload功能:

 if ($this->upload->do_upload($name) {

答案 1 :(得分:1)

你可以检查这个,因为它对我有用。 根据它设置你的消息。

if(!empty($_FILES['gallery_image']['name'][0]))
        {
            $j = 0;    

            for ($i = 0; $i < count($_FILES['gallery_image']['name']); $i++) 
            {
                $target_path = 'images/gallery_images/'; 

                if(!file_exists($target_path))
                {
                    mkdir($target_path);
                }

                $validextensions = array("jpeg", "jpg", "png");
                $ext = explode('.', basename($_FILES['gallery_image']['name'][$i]));   
                $file_extension = end($ext); 
                $target_path = $target_path .$ext[0].'_'.time(). "." . $ext[count($ext) - 1];    
                $j = $j + 1;      

                if (in_array(strtolower($file_extension), $validextensions)) 
                {   
                    if (move_uploaded_file($_FILES['gallery_image']['tmp_name'][$i], $target_path)) 
                    {   

                            $insert_img_query = "INSERT INTO event_gallery (image, event_id) VALUES ('".$ext[0].'_'.time(). "." . $ext[count($ext) - 1]."','".$edited_id."')";
                            $result = mysqli_query($con,$insert_img_query);


                    }
                    else
                    {   

                        header('Location:index.php'); //Redirect your page as per your requirement.
                        exit();
                    }
                } 
                else 
                {

                    header('Location:index.php'); //Redirect your page as per your requirement.
                    exit();
                }
            }

        }

答案 2 :(得分:1)

尝试更改您的代码:

一次转换单个文件中的文件数组

$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];

传递文件名如下:

$this->upload->do_upload('userFile');

也许do_upload函数无法理解$files数组中用于移动文件夹中的数据

希望这有助于你

答案 3 :(得分:0)

使用文件输入名称

更改do_upload函数参数
if ($this->upload->do_upload($name)) {