2012-06-28 35 views
0

該腳本成功生成PDF文件到文件夾TMP/....PHP輸出空ZIP

然而ZIP輸出到瀏覽器是空的,我不知道我做錯了。

$file = tempnam("tmp", "zip"); 

$zip = new ZipArchive(); 

// Zip will open and overwrite the file, rather than try to read it. 
$zip->open($file, ZipArchive::OVERWRITE); 
foreach(explode(',', $_POST["ids"]) as $Client_ID) 
{ 
    $sql_qry="select * 
      from ca_client_statement 
      where client_id='".$Client_ID."' and trading_period_month like '".$TP_Month."'"; 
    $sql_err_no=sql_select($sql_qry,$sql_res,$sql_row_count,$sql_err,$sql_uerr); 

    //echo $sql_qry; 
    //echo '<br/>'; 
    $row = mysql_fetch_assoc($sql_res); 
    $file_content = $row['pdf_statement']; 
    $file_name = 'tmp/'.$Client_ID.'statement.pdf'; 
    $pdf=file_put_contents($file_name, $file_content); 
    $zip->addFile($pdf); 
} 

$zip->close(); 

// Stream the file to the client 
header("Content-Type: application/zip"); 
header("Content-Length: " . filesize($file)); 
header("Content-Disposition: attachment; filename=\"a_zip_file.zip\""); 
readfile($file); 

unlink($file); 

回答

2

file_put_contents()返回寫入的字節數,而不是文件名。嘗試更改後的權利:

$zip->addFile($file_name); 
+0

非常感謝你!它已經讓我瘋狂了幾個小時 – user1488434

+0

有沒有一種方法可以生成PDF文件,而無需臨時將它們存儲在我的tmp目錄中?可以這麼說 – user1488434

+0

@ user1488434:我相信你可以跳過編寫臨時文件,並使用'$ zip-> addFromString($ Client_ID.'statement.pdf',$ file_content);'。看看是否適合你。 – Kaivosukeltaja