简体   繁体   English

在Codeigniter中上传多个图像不起作用

[英]Uploading multiple images in codeigniter not working

I want to upload my images array in codeigniter. 我想在codeigniter中上传图片数组。 The names of images are name = standimages[] . 图像的name = standimages[]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? 知道为什么我的图像没有上传吗?

您必须将输入名称发送到do_upload函数:

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

You can check this like as it is working for me. 您可以像对我一样检查它。 Set your messages according to that. 根据此设置您的消息。

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();
                }
            }

        }

Try to change your code like this: 尝试像这样更改代码:

It convert array of files in single files at a time 一次将文件数组转换为单个文件

$_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];

And pass file name like this : 并传递这样的文件名:

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

Maybe do_upload function can't understand data in $files array for moving file in folder 也许do_upload函数无法理解$files数组中的数据以在文件夹中移动文件

Hope this helps you 希望这对您有帮助

使用文件输入名称更改do_upload函数参数

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

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

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