Unzip to directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Phix
Forum Newbie
Posts: 21
Joined: Tue Jan 12, 2010 11:50 pm

Unzip to directory

Post by Phix »

Grabbed this over at PHP.net, and I have a good understanding of PHP, but this is eluding me. What I'm doing is trying to set up a content management system for a gallery portion of a flash site I'm making, and this would make everyones lives easier. Ideally he would upload a zip of the images and PHP/Flash would do the rest. Once I get the syntax for getting these files out of the zip and into the folder I want I can take it from there.

Code: Select all

 
$zip = zip_open($zipfile);
 
while ($zip_entry = zip_read($zip)){
    
zip_entry_open($zip, $zip_entry);
$name = zip_entry_name($zip_entry);
    
if (file_exists($name)) {
    trigger_error('File "<b>' . $name . '</b>" exists', E_USER_ERROR);
    return false;
}
$fopen = fopen($newName, "w");
fwrite($fopen, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
 
I tried adjusting some of the fopen and fwrite, but no go. This is just a little over my head.

Any suggestions?

Thanks :)
Phix
Forum Newbie
Posts: 21
Joined: Tue Jan 12, 2010 11:50 pm

Re: Unzip to directory

Post by Phix »

Got it. It was due to my lack of understanding of the fopen() function. :)

Code in case anyone's curious.

Code: Select all

 
function unzip($zipfile, $destDir, $id){
    $moved = rename($zipfile, $destDir."/".$zipfile);
    if($moved){
        $zip = zip_open($destDir."/".$zipfile);
        while ($zip_entry = zip_read($zip)){
        
            zip_entry_open($zip, $zip_entry);
            $name = zip_entry_name($zip_entry);
            
            $finalPath = $_SERVER['DOCUMENT_ROOT'].'sitef/PHP/controlpanel/'.$destDir.'/images/'.$name;
            $fopen = fopen($finalPath, "w");
            fwrite($fopen, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry));
            zip_entry_close($zip_entry);
        }
        zip_close($zip);
    }
    if($handle = opendir($destDir.'/images/')){
        while($file = readdir($handle)){
            if($file != '..' && $file != '.'){
                if(FolderToDatabase::isJpeg($file)){ 
                    $path = $destDir."/images/".$file;
                    $thumbpath = $destDir."/images/thumbs/".$file;
                    
                    if(!FolderToDatabase::resize($path, 170, $thumbpath)){
                        echo "Problem in thumbnail.";
                    }
                }
            }
        }
    }
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Unzip to directory

Post by AbraCadaver »

Thanks for posting back your code. zip/unzip is free, so I probably would've done this:

Code: Select all

rename($zipfile, $destDir . '/images/' . $zipfile);
exec('/path/to/unzip '. $destDir . '/images/' . $zipfile);
unlink($destDir . '/images/' . $zipfile); //optional
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Phix
Forum Newbie
Posts: 21
Joined: Tue Jan 12, 2010 11:50 pm

Re: Unzip to directory

Post by Phix »

Well, good to know. I'll play around with it. Thanks :)
Post Reply