Unzip a zip archive on a server with php -
i've tryed ways automatically unzip files php of them failed:
1st variant
<?php function unzip($file){ $zip=zip_open(realpath(".")."/".$file); if(!$zip) {return("unable proccess file '{$file}'");} $e=''; while($zip_entry=zip_read($zip)) { $zdir=dirname(zip_entry_name($zip_entry)); $zname=zip_entry_name($zip_entry); if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="unable proccess file '{$zname}'"; continue; } if(!is_dir($zdir)) mkdirr($zdir,0777); #print "{$zdir} | {$zname} \n"; $zip_fs=zip_entry_filesize($zip_entry); if(empty($zip_fs)) continue; $zz=zip_entry_read($zip_entry,$zip_fs); $z=fopen($zname,"w"); fwrite($z,$zz); fclose($z); zip_entry_close($zip_entry); } zip_close($zip); return $e; } $file = 'file_name.zip'; echo unzip($file);
2nd variant
<?php $zip = zip_open("my_linkedin_groups_scrape_my_run_1_2015.zip"); if ($zip) { while ($zip_entry = zip_read($zip)) { $fp = fopen("./".zip_entry_name($zip_entry), "w"); if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); fwrite($fp,"$buf"); zip_entry_close($zip_entry); fclose($fp); } } zip_close($zip); } ?>
3rd variant
<?php // assuming file.zip in same directory executing script. $file = 'file.zip'; // absolute path $file $path = pathinfo(realpath($file), pathinfo_dirname); $zip = new ziparchive; $res = $zip->open($file); if ($res === true) { // extract path determined above $zip->extractto($path); $zip->close(); echo "woot! $file extracted $path"; } else { echo "doh! couldn't open $file"; } ?>
for 3rd case output doh! couldn't open file.zip
what's wrong? missing?
seems problem write/read rights.
change rights testing purposes on 0777
Comments
Post a Comment