#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Retrieve file from another site and zip it


    HI GUys,

    Noob here.. some help is very much appreciated. There are a few csv files hosted on another website n i'm trying to retrieve them and package into a zip file to download. Zipping seems to work but the package is always empty. Any help is very much appreciated... This is in PHP by the way.
    <?php

    $zip = new ZipArchive();
    $filename = "test114.zip";
    $numParts = count($file_names);
    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE)
    {
    exit("cannot open <$filename>\n");
    }



    $file=/* some URL address hosting this csv file*/".csv";
    $filedata = fopen ($file, "r");
    $contents = fread($filedata, filesize($file));
    $zip->addFile($filedata, "file1");
    fclose($filedata);


    $zip->close();
    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename=\"".$filename."\".zip;");
    header('Content-Length: ' . filesize($filename));
    readfile($filename);

    ?>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,959
    Rep Power
    9397
    PHP Code:
    $zip->addFile($filedata"file1"); 
    Take a closer look at which variables you're using.

    After you've realized the problem, do away with fopen() and such and use file_get_contents instead.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    122
    Rep Power
    0

    Use this Download Headers


    Dear Friend

    I think i can help you
    In case if the external site doesn’t give the read permission then it will return blank zip.
    Use this Download Headers

    PHP Code:
      <?php // HTTP Headers for ZIP File Downloads // http://perishablepress.com/press/2010/11/17/http-headers-file-downloads/  // set example variables $filename = "Inferno.zip"; $filepath = "/var/www/domain/httpdocs/download/path/";  // http headers for zip downloads header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$filename."\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filepath.$filename)); ob_end_flush(); @readfile($filepath.$filename); ?>
    Thanks
    diya

IMN logo majestic logo threadwatch logo seochat tools logo