我試圖使用zip歸檔創建一個zip文件,雖然壓縮文件在兩個不同的文件夾中同時下載兩次,但源代碼位於htdocs文件夾中,瀏覽器設置的默認下載文件夾。有什麼方法可以防止這種情況發生?我只希望它被下載一次到下載文件夾...在兩個不同的文件夾中阻止兩次zip歸檔文件下載兩次
$file_names = explode(',', $_REQUEST['files']);
$dir = $_REQUEST['currentdir'];
//Archive name
$archive_file_name="Downloaded_".date("Y-m-d_G-i-s").".zip";
//Download Files path
$file_path=$dir;
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
$res = $zip->open($archive_file_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if ($res===TRUE) {
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$tt=$file_path."/".$files;
if (file_exists($tt)){
$zip->addFile($tt,$files);
}
else{
return false;
exit;
}
}
$zip->close();
//then send the headers to force download the zip file
header('Content-type: application/zip');
header("Content-Disposition: attachment; filename=\"".basename($archive_file_name)."\"");
header('Pragma: no-cache');
header('Expires: 0');
//header('Content-Length: ' . filesize($archive_file_name));
ob_end_clean();
//flush();
readfile($archive_file_name);
exit;
}
else{
return false;
exit;
}
}
創建在htdocs文件夾中的zip(PHP將其保存在那裏),這就是爲什麼它的存在的原因。你只下載一次。 –
該代碼必須在htdocs中運行它,因爲我所有的php代碼都在htdocs中運行...是否有任何代碼或方式來阻止它生成文件到htdocs中? – Annie
你可以使用一個目錄名稱作爲文件名,然後將其保存在所述目錄中。 –