我可以使用以下代码从本地计算机压缩和下载文件夹。但我想从我的Web服务器下载一个文件夹。我该怎么做。请帮忙。我在谷歌搜索了很多,但我找不到解决方案。
$the_folder = 'C:/Program Files/Red5/webapps/SOSample/streams/';
$zip_file_name = 'getaaa.zip';
$download_file= true;
//$delete_file_after_download= true; doesnt work!!
class FlxZipArchive extends ZipArchive {
// $location="http://localhost/SOSample";
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
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);
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));
readfile($zip_file_name);
//deletes file when its done...
//if ($delete_file_after_download)
//{ unlink($zip_file_name); }
}
?>
答案 0 :(得分:0)
您无法“下载文件夹”。你必须把它压缩起来。
答案 1 :(得分:0)
如上所述,你无法下载文件夹。但是,如果您有文件路径,则可以下载彼此分开的文件。使用file_get_contents可以轻松实现。 http://nl1.php.net/file_get_contents
===============编辑:===============
您需要以递归方式在目录中添加文件。像这样(未经测试):
function createZipFromDir($dir, $zip_file) {
$zip = new ZipArchive;
if (true !== $zip->open($zip_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
return false;
}
zipDir($dir, $zip);
return $zip;
}
function zipDir($dir, $zip, $relative_path = DIRECTORY_SEPARATOR) {
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (file === '.' || $file === '..') {
continue;
}
if (is_file($dir . $file)) {
$zip->addFile($dir . $file, $file);
} elseif (is_dir($dir . $file)) {
zipDir($dir . $file, $zip, $relative_path . $file);
}
}
}
closedir($handle);
}
然后拨打$zip = createZipFromDir('/tmp/dir', 'files.zip');
zip的一些示例,请参阅:http://www.php.net/manual/en/zip.examples.php (代码来自:How to zip a folder and download it using php?)
===============编辑2:===============
根据您的评论:
opendir()用于打开本地目录,因为PHP 5.0.0位于ftp目录下。
如果你的PHP代码在www.domain.com上运行,那么/ pages / to / path实际上是一个本地目录,你可以这样做:
$dir ='<wwwroot>/pages/to/path';
if ($handle = opendir($dir)) {
其中wwwroot是您的php代码所见的文件系统的根目录。
如果您尝试从其他网站下载内容,请尝试例如file_get_contents()函数。请注意,如果远程服务器列出目录的内容,则列表实际上是服务器即时生成的HTML页面。您可能会发现自己需要解析该页面。更好的方法是检查服务器是否提供某种API,以便以标准化形式发回内容,例如,以JSON格式。
答案 2 :(得分:0)
而不是给$ location =“http:// localhost / SOSample”;在Web服务器中提供Web服务器的完整绝对路径,它将从您的Web服务器生成zip文件。您的eb服务器窗口或Linux是否基于它提供了$ location变量的路径。