0

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); }
}
?>
Charles
  • 50,943
  • 13
  • 104
  • 142
Geethanjali
  • 1
  • 1
  • 3

3 Answers3

0

You can't "download a folder." You have to zip it up.

Alex Benoit
  • 105
  • 2
  • 9
  • If you're worried about people having an unzipper, note that Windows can by default open .zip files nowadays. – Alex Benoit Feb 27 '14 at 10:36
  • Yes, I want to zip the folder and download it using php. – Geethanjali Feb 27 '14 at 10:42
  • Actually my concept is, I have a red5 server. All the recordings from the user will be saved in Red5 server in folders with usernames. I want to give a link from which a user can download his recordings – Geethanjali Feb 27 '14 at 10:45
  • "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." You should clarify better. In your post you started off by saying that you were already able to zip up the folder and download it. – Alex Benoit Feb 27 '14 at 10:45
  • http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php – Alex Benoit Feb 27 '14 at 10:46
  • Yes I am already able to zip and download a folder from my local machine using the above code. I want to Zip and download a folder from the web server. – Geethanjali Feb 27 '14 at 10:50
  • No. Its not working. Please help me. I am trying to find a solution from last one month. I googled a lot, everywhere i find a solution to zip and download a folder from local machine. – Geethanjali Feb 27 '14 at 10:57
  • If I change the $the_folder location, i.e, the source location to my web address it downloads an empty zipped folder with the destination name. – Geethanjali Feb 27 '14 at 10:58
0

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 e.g. 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, e.g. in JSON format.

Community
  • 1
  • 1
eL-Prova
  • 1,084
  • 11
  • 27
  • I have the path, but i want the folder to be zipped and then downloaded. – Geethanjali Feb 27 '14 at 10:43
  • @Geethanjali take a look at my edit. If this helps, please mark as answer :) – eL-Prova Feb 27 '14 at 11:03
  • Sorry. It says failed to open dir: not implemented ...in line 15. I have given full rights to the source folder.What else might cause this problem? – Geethanjali Feb 27 '14 at 11:19
  • Yes I am trying to download a folder from another website.I have my web content on one server and red5 installed on another server. So when a user records audio/video on my web server it goes to the red5 server and saves there creating a separate folder for each user. Now when a teacher logs in to the web server, she should be able to download all the recordings of the students/users on single button click. This is the concept. Please help me. I am also new to php. – Geethanjali Feb 28 '14 at 05:44
0

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.

vinoth
  • 101
  • 11
  • My web server is linux based. $location is commented. The actual location is given in $the_folder. I tried giving the complete path but it is not working. It downloads a folder but I cant open it. It says the files are either damaged or corrupted – Geethanjali Feb 27 '14 at 10:48