在 php 中从 Zip 下载中排除隐藏文件
Exclude hidden files from Zip download in php
我在 php 中使用以下代码来压缩文件夹并下载其所有文件:
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/zip/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann
* @access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"$basename\"" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
?>
跳过隐藏文件和文件夹的条件应该加在哪里?我正在使用以下函数来检查 file/folder 是否被隐藏:
function is_hidden_file($path) {
$dir = "\"".$path."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
这是我找到的解决方案,可以跳过实际隐藏文件和其他以“.”开头的文件。和'〜'被压缩和下载。它与 windows 7、window 8 和 windows 8.1 一起工作正常。
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann
* @access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
/************Putting the condition here restricts hidden files and files starting with '.' and '~'...***********************/
if ($file == '.' || $file == '..' || $file[0]=='.' || $file[0]=='~' || is_hidden_file($location.$file)) continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
function is_hidden_file($fn) {
$dir = "\"".$fn."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file){
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}
?>
您好,这里只有一个细节。
如果你想在下载后删除文件需要用这个更新最后一部分代码:
if ($download_file){
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}
我在 php 中使用以下代码来压缩文件夹并下载其所有文件:
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/zip/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann
* @access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"$basename\"" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
?>
跳过隐藏文件和文件夹的条件应该加在哪里?我正在使用以下函数来检查 file/folder 是否被隐藏:
function is_hidden_file($path) {
$dir = "\"".$path."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
这是我找到的解决方案,可以跳过实际隐藏文件和其他以“.”开头的文件。和'〜'被压缩和下载。它与 windows 7、window 8 和 windows 8.1 一起工作正常。
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; @param string $location Real Location;;;; @param string $name Name in Archive;;; @author Nicolas Heimann;;;; @access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; @param string $location Real Location; @param string $name Name in Archive;;;;;; @author Nicolas Heimann
* @access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
/************Putting the condition here restricts hidden files and files starting with '.' and '~'...***********************/
if ($file == '.' || $file == '..' || $file[0]=='.' || $file[0]=='~' || is_hidden_file($location.$file)) continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
function is_hidden_file($fn) {
$dir = "\"".$fn."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO @ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file){
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}
?>
您好,这里只有一个细节。
如果你想在下载后删除文件需要用这个更新最后一部分代码:
if ($download_file){
ob_get_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}