ZipArchive Problem


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:

$archive = 'my_archive_' . time() . '.zip';
$zip = new ZipArchive();
$zip->open($archive, ZIPARCHIVE::CREATE);
foreach ($calls as $conversation){
$url = '\\\\\\\\path\\\	o the\\\\file.mp3';
$zip->addFile($url, $conversation['MessageId'] . '.mp3');

// 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");

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

$url = '\\\\\\\\path\\\	o 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 //// )

Off Topic:

I would bloody love string literals in PHP

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.

@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.

To expand on what Anthony is suggesting:

foreach ($calls as $conversation){
    $url = '\\\\\\\\path\\\	o 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.