SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ZipArchive Problem

    Hi,

    I am experiencing a problem which is preventing me from finishing my current development project.

    I need to zip a number of mp3 files located at various filepaths and enable the user to download the zip file to their system. It appears that the best way of doing this is by using php's ZipArchive class. Whilst working running the script on my localhost I accomplished this quite easily with the following code:

    Code PHP:
    $archive = 'my_archive_' . time() . '.zip';
     
    $zip = new ZipArchive();
    $zip->open($archive, ZIPARCHIVE::CREATE);
     
    foreach ($calls as $conversation){
     
    $url = '\\\\path\\to the\\file.mp3';
     
    $zip->addFile($url, $conversation['MessageId'] . '.mp3');
     
     
    }
     
    $zip->close();
     
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$archive");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
     
    readfile($archive);

    The problem is that when I run the same script on the web server, the zip archive is downloaded but the files are empty. I realise its going to be difficult for anyone to help without knowing the setup I'm using but I hoped somebody might be able to suggest why this might be? If it makes any difference the web server is IIS.

    Many thanks

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,895
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $url '\\\\path\\to the\\file.mp3'
    Looks you developed it on a windows machine and the live server is a *nix machine. Try using forward slashes ( / ) instead of backslashes ( \ )
    Oh, and just one at the time ( / ), not multiple ( // or //// )
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    I would bloody love string literals in PHP
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ScallioXTX Thanks for your reply. I tried that but I'm afraid it didn't work. The web server is also a windows machine but running IIS instead of Apache.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Am I right in thinking these files are located on a network share? If so, does the account which IIS runs under have read permissions to that share?

    Have you tried creating a simple script to read/display the contents of the target directory to ensure you have access? Additionally, try '"echo'ing" out the full UNC target path to see if it is what you expect.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    To expand on what Anthony is suggesting:

    PHP Code:
    foreach ($calls as $conversation){
        
    $url '\\\\path\\to the\\file.mp3';
        if(
    file_exists($url) && is_readable($url) && !is_dir($url)) {
            
    $zip->addFile($url$conversation['MessageId'] . '.mp3');
        } else {
            die(
    "Cannot read file $url !");
        }

    This will test if the file exists, is readable, isn't a directory and tell you something is up if it goes wrong. The dir test may not be necessary, but past experiences have taught me to err on the side of caution.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •