2012-10-18 28 views
0

我做了一個drupal模塊,其中一個模塊的功能是將一些文件壓縮成zip包。它在我的本地環境(xampp)中正常工作,但在服務器上失敗。我的服務器確實啓用了php zip擴展,因爲我可以看到有關php信息的zip信息,我也可以用php解壓縮文件。PHP zip不起作用

此外,我已經chmod文件爲0777。

我的代碼:

$folder = file_directory_path(); 

$zip = new ZipArchive(); 

if ($zip->open('b.zip', ZIPARCHIVE::CREATE) === TRUE) { 

    foreach ($files as $file) { 
     drupal_set_message(t($file)); // I can see the the message on this stpe 
     $zip->addFile($file); 
    } 

    $zip->close(); 
    if (file_exists('b.zip')) { 

     copy('b.zip', $folder . '/b.zip'); 
     unlink('b.zip'); 
     global $base_url; 
     variable_set('zippath', $base_url . $folder . '/b.zip'); 
     drupal_set_message(t('new zip package has been created')); 
    } 
} else { 
    drupal_set_message(t('new zip package failed')); 
} 
+0

失敗時出現錯誤消息是什麼? – Vulcan

+0

@Vulcan根本沒有錯誤信息,thx – user167043

+0

您使用的是PHP和XAMPP的哪個版本? – Baba

回答

1

是..我知道你的意思..這是3可能性

  • 你有寫權限
  • 你沒有使用全路徑
  • 您正在將文件夾包含爲文件

你可以試試這個

error_reporting(E_ALL); 
ini_set("display_errors", "On"); 


$fullPath = __DIR__ ; // <-------- Full Path to directory 
$fileZip = __DIR__ . "/b.zip"; // <--- Full path to zip 

if(!is_writable($fullPath)) 
{ 
    trigger_error("You can't Write here"); 
} 

$files = scandir($fullPath); // <--- Just to emulate your files 
touch($fileZip); // <----------------- Try Creating the file temopary 


$zip = new ZipArchive(); 
if ($zip->open($fileZip, ZIPARCHIVE::CREATE) === TRUE) { 

    foreach ($files as $file) { 
     if ($file == "." || $file == "..") 
      continue; 
     $fileFull = $fullPath . "/$file"; 
     if (is_file($fileFull)) { // <-------------- Make Sure its a file 
      $zip->addFile($fileFull, $file); 
     } 

     // Play your ball 
    } 
    $zip->close(); 
} else { 
    echo "Failed"; 
} 
+0

謝謝!它是zip文件路徑問題,但我不知道它爲什麼在本地環境(xampp)上工作,但沒有在沒有完整路徑的zip服務器上工作。 – user167043

+0

@ user167043很高興我能夠幫助....您的路徑可能已經改變 – Baba

0

我建議你使用rar或zip命令來創建zip。我正在使用Linux,並在我的PHP系統做

$folder = 'your_folder'; // folder contains files to be archived 
$zipFileName = 'Your_file_name'; // zip file name 
$command = 'rar a -r ' . $zipFileName . ' ' . $folder . '/'; 
exec($command); 

它很快。但你需要在你的系統中安裝rar軟件包。

感謝