如何创建和下载没有路径目录的zip文件[重复]

This question already has an answer here:

My code creates a ZIP file and downloads it in this ZIP folder some images exist.

It’s done well but problem is that after I download and extract this folder then all path folder also become my code is:

$id         = $row['id'];
$doc_name   = $row['doc_name'];
$file_names = explode(',',$doc_name);
$file_path  = $_ROOT_REQUIRE."uploads/document/";
$archive_file_name = "demo.zip";
$zip        = new ZipArchive($archive_file_name);
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE ) !== TRUE) {
    echo "cannot open <$archive_file_name>
";
    exit("cannot open <$archive_file_name>
");
}
foreach ($file_names as $files) {
    if ($files !== '') {
        $zip->addFile("$files", basename($files));
    }
} 
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($archive_file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archive_file_name));
header('Cache-Control: private');
ob_clean();
flush();
readfile(basename($archive_file_name));
exit;
</div>

Your Code look's is Fine. Just Replace Your Foreach Loop .

foreach ($files as $file)
{
    $file = str_replace('\\', '/', $file);          
    if(filetype($file) == 'file') {         
        $zip->addFile( $file, pathinfo( $file, PATHINFO_BASENAME ) );
    }
}

it should be work Thanks

A bit unclear on why this would be happening, looking at this line:

$zip->addFile("$files", basename($files));

Why are there " quotes around $files? I would recommend changing that to:

$zip->addFile($files, basename($files));

These are the changes I made and it work well now. Thanks to all.

  1. $overwrite = true;

  2. if($zip->open($archive_file_name,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)

  3. This larger piece:

    foreach ($file_names as $file) {
        $files = $file_path.$file;
        $files = str_replace('\\', '/', $files);          
        if(filetype($files) == 'file') {
            $zip->addFile( $files, pathinfo( $files, PATHINFO_BASENAME ) );
        }
    }