![](/img/trans.png)
[英]How to download a wav file from a web location to local server directory using PHP script
[英]How to download a folder from web server to local machine using php
我可以使用以下代码Zip并从本地计算机下载文件夹。 但我想从Web服务器下载文件夹。 我该怎么做。 请帮忙。 我在Google上搜索了很多,但找不到解决方案。
$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); }
}
?>
您不能“下载文件夹”。 您必须将其压缩。
如前所述,您无法下载文件夹。 但是,如果您有文件路径,则可以下载彼此分开的文件。 使用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/zh/zip.examples.php (代码来自: 如何压缩文件夹并使用php下载它? )
==============编辑2:===============
根据您的评论:
由于PHP 5.0.0在ftp目录上,因此opendir()用于打开本地目录。
如果您的PHP代码在www.domain.com上运行,则/ pages / to / path实际上是本地目录,您可以这样做:
$dir ='<wwwroot>/pages/to/path';
if ($handle = opendir($dir)) {
如您的php代码所示,其中wwwroot是文件系统的根目录。
如果您尝试从其他网站下载内容,请尝试例如file_get_contents()。 请注意,如果远程服务器列出目录的内容,则该清单实际上是服务器动态生成的HTML页面。 您可能会发现自己需要解析该页面。 更好的方法是检查服务器是否提供某种API,以标准格式(例如JSON格式)将内容发回。
而不是给$ location =“ http:// localhost / SOSample”; 提供Web服务器的完整绝对路径,将其放在Web服务器中,它将从Web服务器生成zip文件。 是基于Windows的eb服务器Windows还是linux给出$ location变量的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.