用于下载多个文件的PHP表单,Safari中出现错误:加载失败资源:帧加载中断
我有一个小的wordpress插件,可以在产品中添加下载。在产品页面上,您可以选择多个文件并将其作为zip文件下载 这在Chrome和Firefox中正常工作,但在Safari中,我收到以下错误消息,下载被中断: 加载资源失败:帧加载中断 我的表单如下所示:用于下载多个文件的PHP表单,Safari中出现错误:加载失败资源:帧加载中断,php,wordpress,Php,Wordpress,我有一个小的wordpress插件,可以在产品中添加下载。在产品页面上,您可以选择多个文件并将其作为zip文件下载 这在Chrome和Firefox中正常工作,但在Safari中,我收到以下错误消息,下载被中断: 加载资源失败:帧加载中断 我的表单如下所示: <form name="download-product-files" action="/download-product-files/download-files.php" method=&q
<form name="download-product-files" action="/download-product-files/download-files.php" method="post" download>
// foreach loop through files as input checkboxes
<input type="submit" id="submit-download-product-files" name="createzip" value="Ausgewählte Dateien herunterladen">
</form>
我的donwload-files.php文件如下所示:
if(!isset($_POST['files']) || !isset($_POST['createzip']) || !extension_loaded('zip')) {
exit;
}
require_once("../../../../../../wp-load.php");
$file_paths = array();
// Check if Current Website URL is in files string -> yes: get file path
foreach ($_POST['files'] as $file_url) {
if ( strpos($file_url, get_site_url()) !== false) {
array_push($file_paths, str_replace(get_site_url(), get_home_path(), $file_url));
}
}
if (empty($file_paths) || (!isset($file_paths) && count($file_paths) < 1)) {
exit;
}
ob_start();
$error = ""; //error holder
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = 'tempLED-' . time() . '.zip'; // Zip name
# create a temp file & open it
$tmp_file = tempnam(wp_upload_dir()['basedir'] . '/download-product-files/', '');
$zip->open($tmp_file, ZipArchive::CREATE);
if( !$zip->open($tmp_file, ZipArchive::CREATE) ) {
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
return;
}
# loop through each file
foreach ($file_paths as $file) {
# download file
//$download_file = file_get_contents($file);
#add it to the zip
//$zip->addFromString(basename($file), $file);
$zip->addFile($file, basename($file));
}
$zip->close();
ob_clean();
if(file_exists($tmp_file)) {
// push to download the zip
//header('Content-Type: application/zip');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($tmp_file));
header('Content-Disposition: attachment; filename="tempLED-Produktdaten.zip"');
header('Pragma: no-cache');
header("Expires: 0");
readfile($tmp_file);
// remove zip file is exists in temp path
unlink($tmp_file);
}
ob_flush();
我尝试在一个新标签页中打开表单,但也没有帮助
有什么办法可以解决这个问题吗?如果删除Pragma和Expires标题,会有什么变化吗?没有,同样的错误仍然发生。我发现一些对Safari的引用存在此类问题,但这些都是从2013年开始的老东西。或者2010年,不确定其中描述的场景是否是内容类型的组合:应用程序/八位字节流和内容处置:附件;但是,用户触发操作和获取服务器响应之间的时间间隔仍然没有解决。您不能通过一个请求下载多个文件,像ZIP这样的存档格式几乎是此类内容的唯一选项。如果你在磁盘上创建一个永久性文件,然后在最后重定向到该文件的位置,这是否有效?这或多或少解决了我的问题。我现在创建了扩展名为.zip的文件,创建时使用标题“Location:”重定向。path2url$tmp_文件;下载到该文件,并按预期开始下载。我创建了一个cron作业,每天删除一次临时文件夹中的所有文件,谢谢。