Torquil Beavis
asked on
PHP unzipping script misbehaving
I need to unzip a folder with two child folders, and files in each folder. The folders are created, yet the files are either in the wrong location or not written, as follows. I would appreciate being pointed in the right direction.
/zippo
/folder_1_file_1.txt (this file should not be here - should be in /abc123)
/abc123 (no files - should be folder_1_file_1.txt)
/test_folder_2 (no files - should be folder_2_file_1.txt)
/test_folder_3 (no files - should be folder_3_file_1.txt, folder_3_file_2.txt, folder_3_file_3.txt)
Here is the script:<?php
/* To unzip folders within a zip file */
$file = "test_zip.zip";
$dir = "../../library/zippo/";
$destiny = "../../library/zippo/abc123/";
function Unzip($dir, $file, $destiny) // was $destiny="" when no $destiny in call
{
$path_file = $dir . $file;
$zip = zip_open($path_file);
$_tmp = array();
$count=0;
$count_dir = 0;
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$_tmp[$count]["filename"] = zip_entry_name($zip_entry); // file & it's parent folder (only > /zippo/): XXXXX/filename
$_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry);
$_tmp[$count]["size"] = zip_entry_filesize($zip_entry);
$_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry);
$_tmp[$count]["mtime"] = "";
$_tmp[$count]["comment"] = "";
$_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry)); // Only > /zippo/
$_tmp[$count]["index"] = $count;
$_tmp[$count]["status"] = "ok";
$_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry);
if (zip_entry_open($zip, $zip_entry, "r"))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
if($destiny)
{
$path_file = $destiny . zip_entry_name($zip_entry);
}
else
{
$path_file = $dir . zip_entry_name($zip_entry);
}
$new_dir = dirname($path_file)."/";
if($count > 0)
{
// All but the first file
$count_back = $count - 1;
if($_tmp[$count]["folder"] !== $_tmp[$count_back]["folder"])
{
// The current folder has changed since the previous folder, so make a new folder
mkdir($new_dir);
}
else
{
// The current folder is the same as the previous folder, so ensure no new folder created
}
}
else
{
// The first file only, so ensure the destiny folder is created
mkdir($new_dir);
}
$fp = fopen($dir . zip_entry_name($zip_entry), "w");
fwrite($fp, $buf);
fclose($fp);
zip_entry_close($zip_entry);
}
echo "\n</pre>";
$count++;
}
zip_close($zip);
}
}
Unzip($dir, $file, $destiny);
?>
And the reults:Warning: fopen(../../library/zippo/test_folder_2/): failed to open stream: Is a directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
Warning: fopen(../../library/zippo/test_folder_2/folder_2_file_1.txt): failed to open stream: No such file or directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
Warning: mkdir(): File exists in /.../unzipper_7_1.php on line 52
Warning: fopen(../../library/zippo/test_folder_3/): failed to open stream: Is a directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
Warning: fopen(../../library/zippo/test_folder_3/folder_3_file_1.txt): failed to open stream: No such file or directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
Warning: fopen(../../library/zippo/test_folder_3/folder_3_file_2.txt): failed to open stream: No such file or directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
Warning: fopen(../../library/zippo/test_folder_3/folder_3_file_3.txt): failed to open stream: No such file or directory in /.../unzipper_7_1.php on line 68
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 69
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../unzipper_7_1.php on line 70
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can we please get the test data for this question? Thanks!
ASKER
Thanks guys. Worked ideally. Now I can fill in the dent in the wall ;)
ASKER
So now I'll study the script and learn :)
You are welcome.