chronister Posted May 18, 2008 Share Posted May 18, 2008 Hello, I am playing with manipulating zip files server-side. I have a very simple function that takes 2 parameters, the path of the zip file and the destination. It works great, but I am having trouble figuring out how to the the names / paths of all the files in the zip file. I think I am going to create a photo gallery that is mysql based and I want the ability to upload zip files, grab the names of the files and insert them into the db. The script below is simply a open zip file and extract them to a dir. There is almost no validation in there (coming soon), and there is no error handling. <?php function unzip($file, $dest) { $zip = new ZipArchive; if ($zip->open($file) === TRUE) { if(!is_dir($dest)) { mkdir($dest); } $zip->extractTo($dest); $zip->close(); } else { echo 'Could not open zip file'; } } $file = $_SERVER['DOCUMENT_ROOT'].'/etc/temp/test.zip'; $dest = $_SERVER['DOCUMENT_ROOT'].'/test_dir'; $inflateit = unzip($file, $dest); ?> Any advice on how to get the filenames, sub-dir names and such? Thanks, nate Quote Link to comment Share on other sites More sharing options...
EGONick Posted May 18, 2008 Share Posted May 18, 2008 You'd have to edit your ZipArchive class to return the files from the extractTo function allowing you to use them to do with as you wish, Quote Link to comment Share on other sites More sharing options...
chronister Posted May 18, 2008 Author Share Posted May 18, 2008 The Ziparchive class is a built in class of PHP. http://us3.php.net/zip So I cannot edit the class. What you see in my code is the complete code. There is no other code to it. So that being said, any other ideas, or modifications EGOnick? Thanks for the help, Nate Quote Link to comment Share on other sites More sharing options...
EGONick Posted May 18, 2008 Share Posted May 18, 2008 The actual zip functions allow you to read the contents of the zip file. <?php $zip = zip_open($zipfile); while ($zip_entry = zip_read($zip)) { zip_entry_open($zip, $zip_entry); $name = zip_entry_name($zip_entry); $handle = fopen($name, "w"); fwrite($handle, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)), zip_entry_filesize($zip_entry)); fclose($handle); // use $name to insert in to database. zip_entry_close($zip_entry); } zip_close($zip); ?> Presuming you aren't after uploading a zip file with directories and you can add a directory before the fopen directory. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.