简体   繁体   中英

Change the name of my zip file created with php

I have a script for create zip with images. It works really well !

create_zip($files_to_zip, 'zip/download-'.$id_account.'-'.$time.'.zip');

function create_zip($files = array(),$destination = '',$overwrite = true) {

        if(file_exists($destination) && !$overwrite) { return false; }
        $valid_files = array();
        if(is_array($files)) {
            foreach($files as $file) {
                if(file_exists($file)) {
                    $valid_files[] = $file;
                }
            }
        }
        if(count($valid_files)) {
            $zip = new ZipArchive();
            if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
                return false;
            }
            foreach($valid_files as $file) {
                $zip->addFile($file,$file);
            }
            $zip->close();

            return file_exists($destination);
        }
        else { return false; }
    }

When I download the file, it's well named zip/download-'.$id_account.'-'.$time.'.zip But when I want to extract this, the extract folder is always named files . result

Please do you have any idea for rename my unzipped file to an another name ?

Thanks a lot !

You can use this code to create you zip file.

do like this

$File = 'path' . time() . '.zip';
$zip = new \ZipArchive();
$res = $zip->open($File, \ZipArchive::CREATE);

if ($res === TRUE) {
    $zip->addFile($filename);
    $zip->close();
}
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Length: " . filesize($File));
header("Pragma: no-cache");
header("Expires: 0");
readfile($File);
header("Connection: close");

In your case you just missed header part .
Just add the header part in your code then it will work for you.

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