在PHP中下载Zip文件时显示的路径名和Zip文件名


Path Name and Zip File Name showing when downloading Zip file In PHP

我正在尝试编写一个PHP程序,该程序提供一个可供下载的zip文件。我在互联网上搜索并尝试了所有的解决方案,但我的问题没有得到解决。

我的问题是,当zip文件下载到用户的计算机时,整个路径显示为zip文件的名称。

例如:Zip文件的路径:"http://www.websitename.com/folder1/folder2/"Zip文件名称:"zbc123.Zip"

当浏览器下载zip文件时,文件的名称如下所示:http-_www.websitename.com_folder1_foolder2_zbc123.zip

我不希望路径是下载的zip文件的名称。

我只想显示实际的zip文件。

这是我的代码规范:

$m_item_path_name = "http://www.websitename.com/folder1/folder2/";
$m_zip_file_name = "zbc123.zip"
//Combining the Path and the Zip file name and storing into memory variable
$m_full_path = $m_item_path_name.$m_zip_file_name;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='"".$m_zip_file_name."'"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($m_item_path_name.$m_zip_file_name));
ob_end_flush();
@readfile($m_item_path_name.$m_zip_file_name);

有人能帮我解决这个问题吗。

任何形式的帮助都将不胜感激。

非常感谢。

试试这个:

<?php
$file_names = array('file1.pdf','file2.pdf');
//Archive name
$archive_file_name=$name.'Name_u_want.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/files/';

zipFilesAndDownload($file_names,$archive_file_name,$file_path);
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
        //echo $file_path;die;
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>'n");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files);

    }
    $zip->close();
    //then send the headers to foce download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name");
        header("Content-length: " . filesize($archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
?>