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

在PHP中下载Zip文件时显示的路径名和Zip文件名,php,zip,Php,Zip,我正在尝试编写一个PHP程序,它提供一个zip文件供下载。我在互联网上搜索并尝试了所有的解决方案,但我的问题没有得到解决 我的问题是,当zip文件下载到用户的计算机上时,整个路径显示为zip文件的名称 例如: Zip文件的路径:“http://www.websitename.com/folder1/folder2/" Zip文件名:“zbc123.Zip” 浏览器下载zip文件时,文件名如下: http--www.websitename.com_folder1_folder2_zbc123.zi

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

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

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

浏览器下载zip文件时,文件名如下: http--www.websitename.com_folder1_folder2_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;
}
?>


感谢您的帮助。。。但是zip文件的名称及其路径是通过数据库检索的。。我不想创建一个新的zip文件。。我已经知道路径名和zip文件名。。。。我只想强制下载zip文件。意思是你只想下载一个zip文件?我说的对吗?点击一个链接,就会对数据库进行更新,然后我想强制下载一个zip文件,它。。。。Zip文件名和路径是从数据库中的表中检索的,希望对您有所帮助。。。。。