2011-05-25 23 views
0

我試圖運行一個允許上傳.zip並提取內容的腳本。我抓住了一個在線應用示例代碼,並在開始時添加了一個類b/c我的ISP沒有正確編譯zip功能。PHP上傳和提取郵編

我在卡住的中間留下了很大的評論。不知道這是否與它在IIS上運行有關?

.zip文件被上傳到我所期望的位置,並且我可以手動將其ftp回來並提取文件。我正在尋找這裏提取文件,循環它們,如果它們是圖像文件,然後將它們添加到圖庫圖像的數據庫...首先,但首先。需要理解爲什麼我沒有看到的zip的內容...

<?php // need this bc of ISP settings 
require($_SERVER['DOCUMENT_ROOT']."/_classes/ZipArchive.php"); 
?><?php 
if($_FILES["zip_file"]["name"]) { 
    $filename = $_FILES["zip_file"]["name"]; 
    $source = $_FILES["zip_file"]["tmp_name"]; 
    $type = $_FILES["zip_file"]["type"]; 

    $name = explode(".", $filename); 
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); 
    foreach($accepted_types as $mime_type) { 
     if($mime_type == $type) { 
      $okay = true; 
      break; 
      } 
    } 
    $continue = strtolower($name[1]) == 'zip' ? true : false; 
    if(!$continue) { 
     $message = "The file you are trying to upload is not a .zip file. Please try again."; 
    } 

    // I set up the _TEST dir with 777 permissions 
    $target_path = $_SERVER['DOCUMENT_ROOT']."/_TEST/".$filename; 
    if(move_uploaded_file($source, $target_path)) { 
     $zip = new ZipArchive(); 
     $x = $zip->open($target_path); 

     // ********************************************************** 
     // $x returns an error here 
     // code: ER_OPEN 
     // http://php.net/manual/en/function.ziparchive-open.php 
     // Not sure why????? 
     // ********************************************************** 

     if ($x === true) { 
      $zip->extractTo($_SERVER['DOCUMENT_ROOT']."/_TEST/"); 
      $zip->close(); 
      unlink($target_path); 
      $message = "Your .zip file was uploaded and unpacked."; 
     } 
     else { 
      $message = 'failed'; 
     } 
    } else {  
     $message = "There was a problem with the upload. Please try again."; 
    } 
} 
?> 

回答

0

ISP安裝了必要的zip組件工作,所以一切都很好。感謝@timdream的另一種方法。

1

不是一個解決方案,但解決方法:你有機器的控制?如果是這樣,請安裝7-zip(在Windows上)或unzip,使用system('unzip ...')system('7z ...')解壓縮zip存檔。

+0

謝謝。這是一個IIS框,根據phpinfo()運行「ZipGenius 5」。我會看看如果我可以弄清楚如果一切都失敗,如何通過system()調用來運行這個... – Don 2011-05-26 17:37:20