简体   繁体   中英

php zip archive is created but empty

i try to create a zip archive of pdf files like this:

$path = str_replace('\\', '/',WP_CONTENT_DIR . '/uploads/pdf-files');

$zip = new ZipArchive();

$filename = str_replace('\\', '/', WP_CONTENT_DIR . '/uploads/pdf-files/zip-file.zip');

if($zip->open($filename, ZipArchive::CREATE) !== true) {
    die("Error open");
}

if(is_dir($path)) {
    if($dh = opendir($path)) {
        while(($file = readdir($dh)) !== false) {
            $file_parts = pathinfo($file);

            if($file_parts['extension'] == 'pdf') {
                if($zip->addFile($path . '/' . $file) !== true) {
                    die("Error addFile");
                } else {
                    echo $file . "\n";
                }
            }
        }

        closedir($dh);
    }
}

if($zip->close() !== true) {
    die("Error close");
}

echo content_url('uploads/pdf-files/zip-file.zip');

wp_die();

All expected files echo in the else. But the zip archive is empty. Why?

This code should return an url to the zip archive, for download it via javascript.

In found a solution. If i put a second argument (filename) to addFile it works:

$zip->addFile($path . '/' . $file, $file);

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