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.';
}