简体   繁体   中英

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. But I want to download a folder from my web server. How can i do it. please help. I searched a lot on google but i couldn't find a solution.

$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. 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');

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? )

=============== Edit 2: ===============

Based on your comment:

opendir() is used to open a local directory and since PHP 5.0.0 on an ftp directory.

If your PHP code runs on www.domain.com then /pages/to/path is actually a local directory and you can do this:

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

where wwwroot is the root of the filesystem as seen by your php code.

If you're trying to download content from another website, try eg 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. 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.

Instead of giving $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. Is your eb server windows or linux based on it give the path to $location variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM