我编写了一个脚本,用于将文件添加到zip存档并在表单提交时下载。
文件路径根据提交的数据确定。首先,确定文件路径,然后检查是否存在并添加到数组中。
然后将这些文件添加到压缩存档中,然后下载。以下是代码。这需要太多时间。我将maximum execution time
增加到120 seconds
,但它仍然无法正常工作。
if (isset($valid_files)) {
if(count($valid_files)) {
$zip = new ZipArchive();
$zip_name = "File_name.zip";
$zip_path = "files/$zip_name";
$opened = $zip->open( $zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
if( $opened !== true ){
die("cannot open {$zip_name} for writing.");
}
foreach($valid_files as $key => $file) {
$f_name = $key.ext";
$zip->addFile($file,$f_name);
}
$zip->close();
if (file_exists($zip_path)) {
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/zip" );
header( "Content-Disposition: attachment; filename=\"" . $zip_name . "\"" );
header( "Content-Transfer-Encoding: binary" );
header( "Content-Length: " . filesize( $zip_path ) );
readfile($zip_path);
die;
}
}
}