0

ダウンロード用のzipファイルを配信するPHPプログラムを作成しようとしています。インターネットで検索し、すべての解決策を試しましたが、問題は解決しません。

私の問題は、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);

誰でもこの問題を解決するのを手伝ってくれますか?

あらゆる種類の助けをいただければ幸いです。

どうもありがとう。

4

1 に答える 1

0

これを試して:

<?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;
}
?>
于 2012-09-03T11:43:28.820 に答える