简体   繁体   English

如何使用php将文件夹从Web服务器下载到本地计算机

[英]How to download a folder from web server to local machine using php

I am able to Zip and download the folder from my local machine using the following code. 我可以使用以下代码Zip并从本地计算机下载文件夹。 But I want to download a folder from my web server. 但我想从Web服务器下载文件夹。 How can i do it. 我该怎么做。 please help. 请帮忙。 I searched a lot on google but i couldn't find a solution. 我在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); }
}
?>

You can't "download a folder." 您不能“下载文件夹”。 You have to zip it up. 您必须将其压缩。

As said you cant download a folder. 如前所述,您无法下载文件夹。 However, if you have a file path, you can download files separated from each other. 但是,如果您有文件路径,则可以下载彼此分开的文件。 Using file_get_contents makes it easy. 使用file_get_contents使其变得容易。 http://nl1.php.net/file_get_contents http://nl1.php.net/file_get_contents

=============== Edit: =============== ==============编辑:===============

You need to recursively add files in the directory. 您需要以递归方式在目录中添加文件。 Something like this (untested): 像这样(未经测试):

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);
}

Then call $zip = createZipFromDir('/tmp/dir', 'files.zip'); 然后调用$zip = createZipFromDir('/tmp/dir', 'files.zip');

Some examples of zip, see: http://www.php.net/manual/en/zip.examples.php (code from: How to zip a folder and download it using php? ) 有关zip的一些示例,请参见: http : //www.php.net/manual/zh/zip.examples.php (代码来自: 如何压缩文件夹并使用php下载它?

=============== Edit 2: =============== ==============编辑2:===============

Based on your comment: 根据您的评论:

opendir() is used to open a local directory and since PHP 5.0.0 on an ftp directory. 由于PHP 5.0.0在ftp目录上,因此opendir()用于打开本地目录。

If your PHP code runs on www.domain.com then /pages/to/path is actually a local directory and you can do this: 如果您的PHP代码在www.domain.com上运行,则/ pages / to / path实际上是本地目录,您可以这样做:

$dir ='<wwwroot>/pages/to/path';
if ($handle = opendir($dir)) {

where wwwroot is the root of the filesystem as seen by your php code. 如您的php代码所示,其中wwwroot是文件系统的根目录。

If you're trying to download content from another website, try eg file_get_contents(). 如果您尝试从其他网站下载内容,请尝试例如file_get_contents()。 Note that if the remote server lists the content of a directory the listing is in fact an HTML page generated on the fly by the server. 请注意,如果远程服务器列出目录的内容,则该清单实际上是服务器动态生成的HTML页面。 You may find yourself needing to parse that page. 您可能会发现自己需要解析该页面。 A better approach is to check whether the server offers some sort of API where it sends back the content in a standardized form, eg in JSON format. 更好的方法是检查服务器是否提供某种API,以标准格式(例如JSON格式)将内容发回。

Instead of giving $location="http://localhost/SOSample"; 而不是给$ location =“ http:// localhost / SOSample”; give full absolute path of your web server palce it in your web server and it will make zip file from your web server. 提供Web服务器的完整绝对路径,将其放在Web服务器中,它将从Web服务器生成zip文件。 Is your eb server windows or linux based on it give the path to $location variable. 是基于Windows的eb服务器Windows还是linux给出$ location变量的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP脚本从网站将WAV文件下载到本地服务器目录 - How to download a wav file from a web location to local server directory using PHP script 使用curl PHP将文件从远程文件服务器下载到本地计算机 - Download file from remote file server to local machine using curl php 使用PHP将所有文件从SFTP文件夹下载到本地文件夹 - Download all files from SFTP folder to local folder using PHP 如何将文件从Sftp服务器下载到本地计算机 - How to download file from Sftp server to local machine 如何使用ftp从服务器下载文件以及如何将该文件保存在本地文件夹中 - How to download a file from server using ftp and how to save that file in my local folder 无法使用php函数将文件从本地计算机上传到服务器 - Unable to upload the file from local machine to server using php function 如何使用PHP从服务器文件夹中检索文件并使用javascript在网页上显示/下载文件? - How to retrieve files from server folder using PHP and display/download it on a webpage using javascript? 从Web服务器文件夹下载Excel文件 - Download an Excel file from web server folder 使用php连接到本地计算机上的生产服务器 - Using php connect to production server in local machine 从PHP网页将图像下载到文件夹 - Download images to folder from PHP web page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM