Php Ziparchive排除文件夹&;如何使其通用
这是我在互联网上找到的一段代码,用于创建整个文件夹的.zip文件并下载 我想做一些改变,但我尝试的一切都不起作用。我想要的是:Php Ziparchive排除文件夹&;如何使其通用,php,zip,ziparchive,Php,Zip,Ziparchive,这是我在互联网上找到的一段代码,用于创建整个文件夹的.zip文件并下载 我想做一些改变,但我尝试的一切都不起作用。我想要的是: 使其通用化(即,我希望将makezip.php放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如www.domain.com/files/media/images/它都会得到该文件夹) 下载后删除zip(我认为我做得很好,使用comand取消链接,但我不确定这是正确的方法 删除zip也会获得的和。文件夹 导出名为date.hour.director
- 使其通用化(即,我希望将
放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如makezip.php
它都会得到该文件夹)www.domain.com/files/media/images/
- 下载后删除zip(我认为我做得很好,使用comand
取消链接,但我不确定这是正确的方法
- 删除zip也会获得的
和
文件夹。
- 导出名为
(exdate.hour.directory.zip
)的zip。我尝试过这样做18Sep2013\u 13-26-02\u images.zip
$fd=getcwd();
但它不起作用,它只得到$fd变量$filename=date(“dMY\u H:i:s”).“'.$fd..zip”;
<?php
function download($file) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
$directory = '.';
$files = scandir($directory);
if(empty($files))
{
echo("You haven't selected any file to download.");
}
else
{
$zip = new ZipArchive();
$filename = date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
exit("Cannot open <$filename>\n");
}
$N = count($files);
for($i=0; $i < $N; $i++)
{
$zip->addFile($files[$i], $files[$i]); //add files to archive
}
$numFiles = $zip->numFiles;
$zip->close();
$time = 8; //how long in seconds do we wait for files to be archived.
$found = false;
for($i=0; $i<$time; $i++){
if($numFiles == $N){ // check if number of files in zip archive equals number of checked files
download($filename);
$found = true;
break;
}
sleep(1); // if not found wait one second before continue looping
}
if($found) { }
else echo "Sorry, this is taking too long";
}
?>
好的,我已经自己解决了我的问题,我正在为那些想要的人分享解决方案
- 使其通用(即,我希望将makezip.php放在根文件夹中。我有一个文件管理器,每次我从某个位置调用该脚本时(例如www.domain.com/files/media/images/它获取该文件夹)
我修改了index.php上的代码,并对zip.php(处理压缩过程的文件.Simples solution..Dumb)做了一些更改
1-index.php-只需放置一个简单的链接
<a href="zip.php?dirz=HERE_GOES_THE_FOLDER_URL">Download folder as a .zip</a>
- 下载后删除zip(我想我做得很好,使用comand Unlink,但我不确定这是正确的方法
是的,它是正确的。(在zip.php上)
取消链接($ZIPNAME);
- 删除zip也会得到的.and..文件夹
$direct=array_diff(scandir($directory),array(“.”,“.”,“error_log”);
- 导出名为date.hour.directory.zip(例如18Sep2013_13-26-02_images.zip)的zip文件。我尝试了这样做$fd=getcwd();$filename=date(“dMY_H:I:s”)。“'$fd..zip';但它没有工作,它只获取$fd变量
简单到:
$zipname = date("dMY_H-i-s").'_'.basename($directory).'.zip';
最好的祝愿使用for
循环并将sleep(1)
放入循环中,停止下载文件不会有任何区别,它将继续输出文件内容直到结束。您只能使用设置时间限制(8*60)
设置运行脚本的最大执行时间(但这并不意味着它将在启动8秒后停止,它不计算其他进程获得处理器时间时在内存中花费的空闲时间,也不计算脚本进行的db或系统调用)谢谢Ivan。正如我所说,我对这一点不熟悉。现在寻找其他答案
<?php
function download($file) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
$directory = '.';
$files = scandir($directory);
if(empty($files))
{
echo("You haven't selected any file to download.");
}
else
{
$zip = new ZipArchive();
$filename = date("dMY_H:i:s").'_arquivo.zip'; //adds timestamp to zip archive so every file has unique filename
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
exit("Cannot open <$filename>\n");
}
$N = count($files);
for($i=0; $i < $N; $i++)
{
$zip->addFile($files[$i], $files[$i]); //add files to archive
}
$numFiles = $zip->numFiles;
$zip->close();
$time = 8; //how long in seconds do we wait for files to be archived.
$found = false;
for($i=0; $i<$time; $i++){
if($numFiles == $N){ // check if number of files in zip archive equals number of checked files
download($filename);
$found = true;
break;
}
sleep(1); // if not found wait one second before continue looping
}
if($found) { }
else echo "Sorry, this is taking too long";
}
?>
$zipname = date("dMY_H-i-s").'_'.basename($directory).'.zip';