2013-04-02 28 views
2

我創建了一個代碼來備份整個網站&自動下載一次點擊。這裏是我的代碼如下所示:在PHP中創建ZIP文件提出瀏覽器錯誤,而不是腳本錯誤

if (file_exists("file.zip")) { 
    unlink("file.zip"); 
} 
$folder_array = array(); 
$file_array = array(); 
function listFolderFiles($dir){ 
    global $folder_array; 
    global $file_array; 
    $ffs = scandir($dir); 
    foreach($ffs as $ff){ 
    if ($ff != '.' && $ff != '..') { 
     if (is_dir($dir.'/'.$ff)) { 
     $new_item = "$dir/$ff"; 
     $new_item = str_replace('..//','',$new_item); 
     if ($new_item !== "stats") { 
      array_push($folder_array, $new_item); 
      listFolderFiles($dir.'/'.$ff); 
     } 
     } 
     else { 
     $new_item = "$dir/$ff"; 
     $new_item = str_replace('..//','',$new_item); 
     if (($new_item !== "stats/logs") && ($new_item !== "stats/")) { 
      array_push($file_array, $new_item); 
     } 
     } 
    } 
    } 
} 
listFolderFiles('../'); 
$zip = new ZipArchive; 
if ($zip->open('file.zip', true ? ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE) === TRUE) { 
    foreach($folder_array as $folder) { 
    $zip->addEmptyDir($folder); 
    } 
    foreach($file_array as $key => $file) { 
    $file_path = "../$file"; 
    $zip->addFile($file_path, $file); 
    } 
} 
$zip->close(); 
$file = "file.zip"; 
chmod("$file", 0700); 
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=". $file); 
readfile($file); 

下面這段代碼是工作好一段時間,但在今天看來它不想工作。事情是,這不是一個PHP腳本錯誤。我檢查了我的錯誤日誌,沒有任何顯示。這似乎是一個瀏覽器錯誤,但每個瀏覽器會顯示不同的信息:

鉻說「無法顯示此網頁提供」 火狐說:「連接被重置」 Internet Explorer中說「無法顯示此頁面「

即使出現這些錯誤,ZIP文件仍在創建中,我可以從服務器上下載它。

這裏的事情的清單,我經過廣泛的研究嘗試:

1)我刪除代碼自動具有下載的ZIP文件(所有代碼寫完後我關閉ZIP文件)。仍然會導致瀏覽器錯誤。

2)我看到可能有太多的文件被打開,並且超出我的極限。我將代碼添加到每200個文件後關閉並重新打開ZIP文件。仍然沒有工作。

3)我將文件數量限制爲ZIP。一切工作都很好,低於500.在500到1,000個文件之間,代碼將工作一段時間。有時它會通過罰款,其他人會給我瀏覽器錯誤。大約1000後,它根本無法正常工作。

託管是通過GoDaddy。

PHP版是5.2.17

的max_execution_time被設定爲240(代碼從不此長,通常只需要大約30秒來運行)

memory_limit的被設定在500M(兩倍以上所有文件大小合併)

我很茫然,我真的不知道發生了什麼,因爲這個代碼幾周前對於1500個文件工作得很好。再次,ZIP文件仍在創建中,沒有PHP錯誤,只有瀏覽器返回這些錯誤。

+0

你收到了什麼HTTP響應頭?即使您的腳本只運行30秒,這聽起來像是超時了,也許GoDaddy設置了全局max_time?是否正在談論他們的支持? – hammus

+0

它實際上沒有給出任何響應標題。舉例來說,這是Chrome正在生產的產品: 此網頁不可用 *****中的網頁可能會暫時關閉,或者它可能已永久移動到新的網址。 錯誤103(net :: ERR_CONNECTION_ABORTED):未知錯誤。 –

+0

壓縮文件是否保存在您要壓縮的目錄中? – tftd

回答

1

我不知道你的代碼有什麼問題,但我使用下面的代碼,它一直與我一起工作。

function create_zip($path, $save_as) 
{ 
    if (!extension_loaded('zip')) 
     throw new ErrorException('Extension ZIP has not been compiled or loaded in php.'); 
    else if(!file_exists($path)) 
     throw new ErrorException('The file/path you want to zip doesn\'t exist!'); 


    $zip = new ZipArchive(); 
    if (!$zip->open($save_as, ZIPARCHIVE::CREATE)) 
     throw new ErrorException('Could not create zip file!'); 

    $ignore = array('.','..'); 
    if($path == dirname($save_as)) 
     $ignore[] = basename($save_as); 

    $path = str_replace('\\', '/', realpath($path)); 

    if (is_dir($path)) { 
     $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); 
     foreach ($files as $file) { 
      $file = str_replace('\\', '/', $file); 

      if(in_array(substr($file, strrpos($file, '/')+1),$ignore)) continue; 

      $file = realpath($file); 
      if (is_dir($file)) { 
       $zip->addEmptyDir(str_replace($path . '/', '', $file . '/')); 
      } 
      else if (is_file($file)) { 
       $zip->addFromString(str_replace($path . '/', '', $file), file_get_contents($file)); 
      } 
     } 
    } 
    else if (is_file($path)) { 
     $zip->addFromString(basename($path), file_get_contents($path)); 
    } 

    return $zip->close(); 
} 

$zip = create_zip('/path/to/your/zip/directory', '/path/to/your/save.zip'); 

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="'.basename('/path/to/your/save.zip').'"'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize('/path/to/your/save.zip')); 
echo readfile('/path/to/your/save.zip'); 
0

instantate一個迭代器(創建ZIP壓縮文件之前,以防萬一zip文件的源文件夾內創建),並遍歷目錄來獲取文件列表。

See More Code At:click me

相關問題