簡體   English   中英

PHP - 用戶如何設置下載文件的保存目錄?

[英]PHP - How user can set directory where downloaded file will be saved?

我正在制作一個簡單的應用程序,其中用戶使用 2 HTML 輸入:文件字段上傳文件,並從中創建一個 zip 存檔。 有什么辦法可以讓用戶選擇這個zip可以go在哪里? (類似於“另存為”窗口)。

這是我當前的解決方案,它只將存檔保存到默認下載目的地而不詢問。

    $files = array($_FILES["fileone"]["name"], $_FILES["filetwo"]["name"]);
    $zipname = "myarchive.zip";
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile($zipname);

您需要使用標頭強制下載:

$file = 'somefile.zip';

if(!file)
{
    // File doesn't exist, output error
    die('file not found');
}
else
{
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");

    // Read the file from disk
    readfile($file);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM