简体   繁体   中英

Codeigniter's zip download files are empty

I have a page where users can choose which files they want to download from different jobs. There could be several files for each job. When the form is submitted, the files are sent to a function that should zip them in the different job folders and then zip the whole lot and download it.

Everything is working great except that when I go into each folder in the downloaded zip file, it shows me that there is an array in there, but it's empty. I'm not sure what I'm doing wrong.

Here is the function:

public function downloadDocFiles() {
    $postdata = $this->input->post();
    $this->load->library('zip');
    $this->load->library('awss3', null, 'S3');
    foreach ($postdata['files'] as $key => $value) {
        $d = $this->S3->readFile('uploads/' . $key . '/' . $value, true);
        $this->zip->add_data($key . '/' . $value, $d);
    }
    $this->zip->download('Completed Jobs.zip');
}

The data comes through as:

Array
    (
        [files] => Array
            (
                [3454] => Array
                    (
                        [0] => file1.docx
                        [1] => file2.docx
                    )

                [2711] => Array
                   (
                        [0] => nameoffile.docx
                   )

                [1162] => Array
                   (
                        [0] => zyx3.docx
                        [1] => iure8.docx
                   )

             )

)

The file, Completed Jobs.zip is downloaded and has folders inside it (3454, 2711 and 1162) but those folders just contain 'Array', size 0.

Thanks to Omas Abbas for the help.

I changed my code to this and it worked:

public function downloadDocFiles() {
    $postdata = $this->input->post();
    $this->load->library('zip');
    $this->load->library('awss3', null, 'S3');
    foreach ($postdata['files'] as $key => $value) {
        $count = count($postdata['files'][$key]);
        for ($i = 0; $i < $count; $i++) {
            $d = $this->S3->readFile('uploads/' . $key . '/' . $value[$i], true);
            $this->zip->add_data($key . '/' . $value[$i], $d);
        }
    }
    $this->zip->download('Completed Jobs.zip');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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