簡體   English   中英

使用php在服務器上解壓縮zip存檔

[英]Unzip a zip archive on a server with php

我嘗試了一些使用php自動解壓縮文件的方法,但所有方法均失敗:

第一變種

<?php
function unzip($file){
  $zip=zip_open(realpath(".")."/".$file);
  if(!$zip) {return("Unable to 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 to 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);

第二變體

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

第三變體

<?php 
// assuming file.zip is in the same directory as the executing script.
$file = 'file.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
  // extract it to the path we determined above
  $zip->extractTo($path);
  $zip->close();
  echo "WOOT! $file extracted to $path";
} else {
  echo "Doh! I couldn't open $file";
}
?>

第三種情況的輸出是Doh! I couldn't open file.zip Doh! I couldn't open file.zip

怎么了? 我缺少什么嗎? 一世

似乎有寫/讀權限問題。
出於測試目的,將權限更改為0777

我會選擇第三種。 嘗試使用絕對路徑來壓縮文件並在錯誤消息中轉儲$ res。 它會說出到底是什么問題,只需將其與特定的錯誤代碼http://php.net/manual/en/ziparchive.open.php進行比較即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM