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)

Open in new window

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);
?>

Open in new window

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

Open in new window

Torquil BeavisBusinessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Any reason you can't do it like this?
<?php
/* To unzip folders within a zip file */
$file = "test_zip.zip";
$dir = "../../library/zippo/";
$destiny = "../../library/zippo/abc123/";
$path_file = $dir . $file;
$zip = new ZipArchive;
$res = $zip->open($path_file);
if ($res === TRUE) {
  $zip->extractTo($destiny);
  $zip->close();
  echo 'Successfully extracted';
} 
else {
  echo 'There was a problem';
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Can we please get the test data for this question? Thanks!
0
Torquil BeavisBusinessAuthor Commented:
Thanks guys. Worked ideally. Now I can fill in the dent in the wall ;)
0
Torquil BeavisBusinessAuthor Commented:
So now I'll study the script and learn :)
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.