1
votes

I have to file upload operation consecutive, first for images like gif|jpg|jpeg|png|svg and the second psd|rar|zip|doc|word|txt|xlsx|pdf First one is working just fine, i can upload all images but the second, i can not upload any of these types but when i try to upload image on second segment it works.

if (isset($_FILES['content_images']['name'])) {
    $count_files = count($_FILES['content_images']['name']);

    for ($i = 0; $i < $count_files; $i++) {
        $_FILES['image']['name'] = $_FILES['content_images']['name'][$i];
        $_FILES['image']['type'] = $_FILES['content_images']['type'][$i];
        $_FILES['image']['tmp_name'] = $_FILES['content_images']['tmp_name'][$i];
        $_FILES['image']['error'] = $_FILES['content_images']['error'][$i];
        $_FILES['image']['size'] = $_FILES['content_images']['size'][$i];
        $config_images['upload_path'] = "./public/site/images/contents";
        $config_images['allowed_types'] = 'gif|jpg|jpeg|png|svg';
        $config_images['max_size'] = 5000;
        $config_images['max_width'] = 7680;
        $config_images['max_height'] = 4320;
        $this->load->library("upload", $config_images);

        if (!$this->upload->do_upload('image')) {
            echo $this->upload->display_errors();
            exit;
        } else {
            $data = $this->upload->data();
            $path_images[] = "public/site/images/contents/".$data['file_name'];
        }
    }
}

if (isset($_FILES['content_files']['name'])) {
    $count_files=count($_FILES['content_files']['name']);

    for ($i = 0; $i < $count_files; $i++) {
        $_FILES['file']['name'] = $_FILES['content_files']['name'][$i];
        $_FILES['file']['type'] = $_FILES['content_files']['type'][$i];
        $_FILES['file']['tmp_name'] = $_FILES['content_files']['tmp_name'][$i];
        $_FILES['file']['error'] = $_FILES['content_files']['error'][$i];
        $_FILES['file']['size'] = $_FILES['content_files']['size'][$i];
        $config_files['upload_path'] = "./public/site/files/contents";
        $config_files['allowed_types'] = 'psd|rar|zip|doc|word|txt|xlsx|pdf';
        $config_files['max_size'] = 5000;
        $config_files['max_width'] = 7680;
        $config_files['max_height'] = 4320;
        $this->load->library("upload",$config_files);

        if (!$this->upload->do_upload('file')) {
            foreach($path_images as $p){
                unlink($p);
            }
            echo $this->upload->display_errors();
            exit;
        } else {
            $data=$this->upload->data();
            $path_files[] = "public/site/files/contents/".$data['file_name'];
        }
    }
}
1

1 Answers

0
votes

I found the solution For help : You should load the library at the top for 1 time only, and then u should initialize it inside 'if condition'. When you load upload library and config array inside first condition, when you pass to second condition upload library had already loaded and uses the first condition's config array.

$this->load->library("upload");
            if(isset($_FILES['content_images']['name'])){
                $count_files=count($_FILES['content_images']['name']);
                for($i = 0;$i<$count_files;$i++){
                    $_FILES['image']['name'] = $_FILES['content_images']['name'][$i];
                    $_FILES['image']['type'] = $_FILES['content_images']['type'][$i];
                    $_FILES['image']['tmp_name'] = $_FILES['content_images']['tmp_name'][$i];
                    $_FILES['image']['error'] = $_FILES['content_images']['error'][$i];
                    $_FILES['image']['size'] = $_FILES['content_images']['size'][$i];
                    $config_images['upload_path'] = "./public/site/images/contents";
                    $config_images['allowed_types'] = 'gif|jpg|jpeg|png|svg';
                    $config_images['max_size'] = 5000;
                    $config_images['max_width'] = 7680;
                    $config_images['max_height'] = 4320;
                    $this->upload->initialize($config_images);
                    if(!$this->upload->do_upload('image')){
                        echo $this->upload->display_errors();
                        exit;
                    }else{
                        $data=$this->upload->data();
                        $path_images[] = "public/site/images/contents/".$data['file_name'];
                    }
                }
            }
            if(isset($_FILES['content_files']['name'])){
                $count_files=count($_FILES['content_files']['name']);
                for($i = 0;$i<$count_files;$i++){
                    $_FILES['file']['name'] = $_FILES['content_files']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['content_files']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['content_files']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['content_files']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['content_files']['size'][$i];
                    $config_files['upload_path'] = "./public/site/files/contents";
                    $config_files['allowed_types'] = 'psd|rar|zip|doc|word|txt|xlsx|pdf';
                    $config_files['max_size'] = 5000;
                    $config_files['max_width'] = 7680;
                    $config_files['max_height'] = 4320;
                    $this->upload->initialize($config_files);
                    if(!$this->upload->do_upload('file')){
                        foreach($path_images as $p){
                            unlink($p);
                        }
                        echo $this->upload->display_errors();
                        exit;
                    }else{
                        $data=$this->upload->data();
                        $path_files[] = "public/site/files/contents/".$data['file_name'];
                    }
                }
            }