Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZipArchive: `failed to open stream: Is a directory`

I keep getting a failed to open stream: Is a directory error while trying to unzip a file with PHP.

This is usually an issue with the destination path.

However, in my case, the destination is fine: I can unzip different files using the same code, or unzip the same file without PHP.

The ZipArchive::extractTo documentation did not help. Nor did other SO threads on this error message.


FYI, this is a Q&A post.


Here is the source code as requested in a comment. I shared the Dropbox link publicly, seeing as this matter is dependent on the format of the ZIP input (/ path in ZIP file, see answer.)

/** Config **/
$url = 'https://www.dropbox.com/sh/nx5792q9syppaoo/AAAJZBWlGNAd_EkmQaFvEwe0a?dl=1';

/** Where to save **/
$zipped = ABSPATH . 'tmp/updates/dialogues.zip';    /* Keep in mind this is public-facing. */
$outputdirectory = ABSPATH . 'tmp/updates/testing'; /* The trailing slash is optinal. */

/** Download the dialogues **/
echo 'Fetching the dialogues...<br>';
if ( ( $dropboxfile = fopen( $url, 'r' ) ) === FALSE ) {    /* == has higher precedent over = */
    echo 'FATAL ERROR: could not open Dropbox file.';
    die();
} else if ( file_put_contents( $zipped, $dropboxfile ) === FALSE ) {
    echo 'FATAL ERROR: could not copy Dropbox file.';
    die();
}
echo "Dialogues downloaded as zip OK. (See {$zipped})<br>";


/** Unzip the dialogues **/
print 'Unzipping...<br>';
$zip = new ZipArchive;
$res = $zip->open( $zipped );
if ( $res === TRUE ) {

    // Does not work
    // $zip->extractTo( ABSPATH . 'tmp/updates/testing' );

    // Works
    for( $i = 0 ; $i < $zip->numFiles ; $i++ ) {
        if ( $zip->getNameIndex( $i ) != '/' && $zip->getNameIndex( $i ) != '__MACOSX/_' ) {
            print $zip->getNameIndex( $i ) . '<br>';
            $zip->extractTo( $outputdirectory, array($zip->getNameIndex($i)) );
        }
    }

    $zip->close();
    print 'Done unzipping.<br>';
} else {
    print 'FATAL ERROR: unzipping failed.';
}
like image 765
Fabien Snauwaert Avatar asked Aug 22 '15 14:08

Fabien Snauwaert


1 Answers

As it turned out, I had a / "folder" inside the ZIP archive.

This is confirmed by browsing the ZIP archive through the command-line. e.g., on OS X, using unzip -vl dialogues.zip gave me this:

Archive:  dialogues.zip
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
--------  ------  ------- -----   ----   ----   ------    ----
       0  Defl:N        2   0%  08-22-15 12:07  00000000  /
    2154  Defl:N     1064  51%  06-08-15 15:18  602c6793  dialogue001-en.txt
    ...
    4379  Defl:N     1793  59%  08-21-15 13:56  75e1ef52  dialogue106-en.txt
      70  Defl:N       39  44%  08-22-15 12:07  985af458  __MACOSX/_
--------          -------  ---                            -------
  775512           343864  56%                            312 files

The file is downloaded from Dropbox and I'd rather be able to work with it as it is.

So I just ended up filtering the list of files contained inside the ZIP archive, before deciding to unzip any given file:

print 'Unzipping...<br>';
$zip = new ZipArchive;
$res = $zip->open( $zipped );
if ( $res === TRUE ) {
    // 
    for( $i = 0 ; $i < $zip->numFiles ; $i++ ) {
        if ( $zip->getNameIndex( $i ) != '/' && $zip->getNameIndex( $i ) != '__MACOSX/_' ) {
            print $zip->getNameIndex( $i ) . '<br>';
            $zip->extractTo( $tempdirectory, array($zip->getNameIndex($i)) );
        }
    }
    $zip->close();
    print 'Done unzipping.<br>';
} else {
    print 'FATAL ERROR: unzipping failed.';
}

Did not see this documented anywhere and wasted close to an hour on this. I hope this will help someone.

I'm still not clear as to whether the / reference inside of the ZIP file is correct. (Comments welcome.)

like image 57
Fabien Snauwaert Avatar answered Oct 20 '22 05:10

Fabien Snauwaert