0

scroll down for an update posted about 40 minutes after the original question was asked

I have some PHP code that, until April 19, worked flawlessly. I have made no changes to the code, but it has stopped working. All I am trying to do, is extracting a zip archive using ZipArchive. I am using PHP version 7.2.34

What I have done before asking this question is:

  • Made sure the zip-file exists - it does.
  • Made sure that no permissions have changed. They have not. Both source and destination directories have full permissions.
  • Made sure the zip-file is not corrupted. As far as I can see, it is not. zip -T gives "test of O77728.zip is OK"
  • Extracted the files from the zip file using both the linux commandline command and downloaded the archive and double-clicked it. Both opens the archive just fine.
[root@blahblah foo]# unzip O77728.zip
Archive:  O77728.zip
   creating: O77728/
  inflating: O77728/O77728_152203_Q200_Bildkalibrering.pdf
  inflating: O77728/O77728_152203_Q200_Bildkalibrering.xml
  inflating: O77728/O77728_152204_Q100_Spelklar.pdf
  inflating: O77728/O77728_152204_Q100_Spelklar.xml
  • Checked the version control to make sure that no one else has changed something in the code that I was unaware of. No, the code is intact. No changes since way before April 19.

Now, on to the code:

As you can see, it is totally basic.

function prepareArchivedFiles($zipFile) {
    $zip = new ZipArchive();
    $tmpDir = basename($zipFile,".zip");
    $res = $zip->open($zipFile, ZipArchive::CHECKCONS);
    if ($res !== TRUE) {
        error_log("error opening $zipFile: " . $zip->getStatusString());
        switch($res) {
            case ZipArchive::ER_EXISTS:
                error_log("File already exists");
            case ZipArchive::ER_INCONS :
                error_log("Consistency check failed");
            case ZipArchive::ER_INVAL:
                error_log("Invalid argument");
            case ZipArchive::ER_MEMORY:
                error_log("Malloc failure");
            case ZipArchive::ER_NOENT:
                error_log("No such file");
            case ZipArchive::ER_NOZIP:
                error_log("Not a zip archive");
            case ZipArchive::ER_OPEN:
                error_log("Cannot open file");
            case ZipArchive::ER_READ:
                error_log("Read error");
            case ZipArchive::ER_SEEK:
                error_log("Seek error");
            case ZipArchive::ER_CRC :
                error_log("checksum failed");
            default:
                error_log("error " . $res);
        }
        return false;
    } else {
        if (!file_exists($GLOBALS["mounts"]["abc"]."__XML_Import_Archive/abc_temp_files/"))
            mkdir($GLOBALS["mounts"]["abc"]."__XML_Import_Archive/abc_temp_files/",0777,true);
        if (!$zip->extractTo($GLOBALS["mounts"]["abc"]."__XML_Import_Archive/abc_temp_files/")) {
            error_log("Could not extract $zipFile");
            return false;
        }
        $zip->close();
        unlink($zipFile);
        return $tmpDir;

    }

When trying to echo out getStatusString to the error log, it tells me this, which in turn leads me to believe that $zip->open failed since zip is invalid or uninitialized.

PHP Warning:  ZipArchive::getStatusString(): Invalid or uninitialized Zip object in /var/www/html/sites/abc/import.php on line 209

The switch spews out the following - the only one that is not triggering is "file already exists":

Consistency check failed
Invalid argument
Malloc failure
No such file
Not a zip archive
Cannot open file
Read error
Seek error
checksum failed
error 21

Error 21 means "archive inconsistent" but I don't know what to do with that.

UPDATE I failed to mention that the zip archives are exported from an external system to my system.

Now, I tried this:

  • I downloaded one of the zip-files from the server to my mac.
  • I unzipped it on my desktop. Example.zip -> Example (a folder containing the XML and PDFs)
  • I changed the name of the folder to Example2 and archived it to Example2.zip
  • I uploaded Example2.zip to the server and let the import script run.

I got the same errors again.

Then I deleted Example2.zip from my mac and archived the Example-folder again. This time to Example3.zip.

Before I uploaded it to the server, I deleted the hidden MACOS resource files and whatever, like this: foo@FOO-8 Desktop % zip -d O77799.zip "__MACOSX*"

Now the import script worked perfectly!

This leads me to believe that there is something wrong with the original zip-files from the external system. Not sure what, though.

SECOND UPDATE

I decided to try AGAIN...

I downloaded O77727.zip from the server to my mac again. I extracted the zip, which created a folder with the same name (O77727). I renamed the folder O77728 and right clicked it to make a new zip-file which was called O77728.zip I renamed the folder again, this time to O77729 and right clicked it to make another zip file, this time called O77729.zip

I uploaded O77728.zip and O77729.zip to the server. Now, we have three zip-files on the server: O77727.zip, O77728.zip and O77729.zip

Then I used the zip command with -d to remove the special Mac crap files from O77729.zip like this: zip -d O77729.zip "__MACOSX*"

Now we have the original zip file, we have one unaltered remade zip file and we have one zip file in which I have removed some Mac-stuff.

Running zip -sf on the three zip-files we see this:

[root@blachblach]# zip -sf O77727.zip
Archive contains:
  O77727/
  O77727/O77727_152198_Q100_Maattband 1m.pdf
  O77727/O77727_152198_Q100_Maattband 1m.xml
  O77727/O77727_152199_Q10_Forinstallerad Dator!.pdf
  O77727/O77727_152199_Q10_Forinstallerad Dator!.xml
  O77727/O77727_152200_Q10_akta Bildkalibreing.pdf
  O77727/O77727_152200_Q10_akta Bildkalibreing.xml
  O77727/O77727_152201_Q200_Service jag har anvants.pdf
  O77727/O77727_152201_Q200_Service jag har anvants.xml
  O77727/O77727_152202_Q200_Service jag har oppnats.pdf
  O77727/O77727_152202_Q200_Service jag har oppnats.xml
Total 11 entries (472109 bytes)
[root@stewiegriffin]# zip -sf O77728.zip
Archive contains:
  O77728/
  __MACOSX/._O77728
  O77728/O77727_152200_Q10_akta Bildkalibreing.xml
  O77728/O77727_152200_Q10_akta Bildkalibreing.pdf
  O77728/O77727_152198_Q100_Maattband 1m.xml
  O77728/O77727_152198_Q100_Maattband 1m.pdf
  O77728/O77727_152202_Q200_Service jag har oppnats.pdf
  O77728/O77727_152202_Q200_Service jag har oppnats.xml
  O77728/O77727_152201_Q200_Service jag har anvants.xml
  O77728/O77727_152201_Q200_Service jag har anvants.pdf
  O77728/O77727_152199_Q10_Forinstallerad Dator!.pdf
  O77728/O77727_152199_Q10_Forinstallerad Dator!.xml
Total 12 entries (472329 bytes)
[root@wildatang]# zip -sf O77729.zip
Archive contains:
  O77729/
  O77729/O77727_152200_Q10_akta Bildkalibreing.xml
  O77729/O77727_152200_Q10_akta Bildkalibreing.pdf
  O77729/O77727_152198_Q100_Maattband 1m.xml
  O77729/O77727_152198_Q100_Maattband 1m.pdf
  O77729/O77727_152202_Q200_Service jag har oppnats.pdf
  O77729/O77727_152202_Q200_Service jag har oppnats.xml
  O77729/O77727_152201_Q200_Service jag har anvants.xml
  O77729/O77727_152201_Q200_Service jag har anvants.pdf
  O77729/O77727_152199_Q10_Forinstallerad Dator!.pdf
  O77729/O77727_152199_Q10_Forinstallerad Dator!.xml
Total 11 entries (472109 bytes)

As you can see, O77727.zip and O77729.zip seem identical. They have the same byte file size.

Now, I ran the script. And both O77727.zip and O77728.zip fails. But O77729.zip works!!! Why, oh dear lord, why?

My own conclusion is that there is nothing wrong with my code, and that something has changed in the function that creates the zip-file in the first place, but I cannot see it. My first remade zip fails because of the Mac-stuff. My second remade zip works because it is as it should be.

  • But your output of extracting `077728.zip` shows just document files. Are you sure this the library you use? – mardubbles May 24 '22 at 09:19
  • Hello Mardubbles. Not entirely sure what you mean, but I just made a discovery. I am adding this to the original question. – Per Jansson May 24 '22 at 09:54
  • is the zip file uploaded to your php app or is it transferred using sftp (or something similar)? if its sftp, you should check whether the zip files are transfered as ascii or binary, see [relevant discussion](https://stackoverflow.com/questions/34074050/ziparchive-unable-to-open-zip-file-only-in-php). – Bagus Tesa May 24 '22 at 11:34
  • Bagus - that is absolutely a possibility. The upload is certainly transferred via SFTP. Unfortunately, that is nothing I have control over. I will check with the "other side" at once! Thank you! – Per Jansson May 24 '22 at 12:19
  • I still don't see how PDF and XML files create a coding dependency. XML maybe but coinciding with same names on the PDFs indicates that these are likely documentation files. – mardubbles May 24 '22 at 21:29
  • *My own conclusion is that there is nothing wrong with my code, and that something has changed* ... that is what likely everyone agrees on. I just think the "zip" file you are looking at is a dead end. That file looks like documentation, not a library/api. – mardubbles May 24 '22 at 21:33

0 Answers0