Take the 2-minute tour ×

Here's a very stripped down version of the code I'm using.

$url = "http://server.com/getDaFile";

//Get the file from the server
$zip_file_contents = file_get_contents($url);
//Write file to disk
file_put_contents("file.zip", $zip_file_contents);

//open zip file
$zip = zip_open("file.zip");

    while($zip_entry = zip_read($zip))
        if(zip_entry_open($zip, $zip_entry, 'r'))
            //Read the whole file
            $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

            Do stuff with $buf!!!


    echo "Not a resource. Oh noes!\n";

So : get the file, save it to disk, unzip it to extract files it contains, do stuff with files. The problem here is that, for some reason I cannot figure out, zip_read returns FALSE, as if it couldn't read files inside the ZIP archive. $zip does contain a resource, I've checked with var_dump.

What makes this even stranger is that I downloaded the ZIP file on my PC using the URL on top, manually uploaded it to the PHP server, and commented out the calls to file_get_contents and file_put_contents so PHP uses the local version. When I do this, zip_read correctly finds the right amount of files inside the ZIP and processing proceeds as it should.

I also tried doing this : $zip = zip_open($url) but $zip fails the is_resource($zip) check.

Something is obviously wrong with my code since the URL works and returns a valid ZIP archive. What is it?

share|improve this question
did you bother checking if f_g_c() actually got something, like echo strlen($zipfile_contents)? That fact that it works on your pc doesn't mean much. totally different environments. –  Marc B Dec 4 '13 at 21:15
Your comment about different environments is right, @MarcB. As for $zipfile_contents, echoing its contents displays a large amount of gibberish (as expected from a ZIP file), but that gibberish contains the name of the files I'm looking for towards the end. –  SolarBear Dec 4 '13 at 21:20
zip_open returns either a resource or an error number. you should have if is_resource() { else echo $zip } type thing to see what that error number is (if any). don't see anything in the online docs what the codes are, but they'd be in the php-zip source somewhere. –  Marc B Dec 4 '13 at 21:24
Edited code accordingly (I even added a var_dump on my side inside the if is_resource() check to make sure, it displays resource(161, Zip Directory)) and the check does pass, so it really is a resource I'm dealing with. –  SolarBear Dec 4 '13 at 21:30
It seems like the ZIP file is getting corrupt. Can you run an md5_file() on the file that is downloaded by the script and compare the hash with that of the version you downloaded yourself. Can you also provide the real file URL for us to test? –  diolemo Dec 4 '13 at 21:31

1 Answer 1

up vote 0 down vote accepted

So I finally found out the problem. Following @diolemo's suggestion, I opened my server's ZIP archive in a hex editor. Here's what I found at the top, followed by the usual ZIP binary data : http://pastebin.com/vQEXJtTN

It turns out there was a PHP error mixed in with the actual content of the ZIP file. Unsure of how to fix this (but knowing it certainly had to do with HTTP headers), I tried this guy's code and, what do you know, my code works perfectly now!

Lessons learned? Never trust your data, even if it seems all right (both 7-Zip and Winrar managed to open the file without problem).

share|improve this answer

Your Answer


