![](/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.