2012-10-30 85 views
3

有託管在其他網站上的幾個CSV文件,N I正在努力找回它們並封裝成一個zip文件下載。壓縮似乎工作,但包總是空的。任何幫助是非常讚賞...這是PHP的方式。從其他網站下載文件,並添加到壓縮

<?php 

$zip = new ZipArchive(); 
$filename = "test114.zip"; 
$numParts = count($file_names); 
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) 
{ 
    exit("cannot open <$filename>\n"); 
} 



$file="http://ichart.finance.yahoo.com/table.csv?s=YHOO&d=9&e=25&f=2012&g=d&a=0&b=3&c=2000&ignore=.csv"; 
$filedata = fopen ($file, "r"); 
$contents = fread($filedata, filesize($file)); 
$zip->addFile($filedata, "file1"); 
fclose($filedata); 


$zip->close(); 
header('Content-Type: application/zip'); 
header("Content-Disposition: attachment; filename=\"".$filename."\".zip;"); 
header('Content-Length: ' . filesize($filename)); 
readfile($filename); 

?> 

回答

0

試圖從該行刪除擴展:

header("Content-Disposition: attachment; filename=\"".$filename."\";"); 

你已經設置的第二行(代碼)的文件擴展名。

0

試試這個,確保你有寫權限在當前工作目錄

<?php 

$zip = new ZipArchive(); 
$filename = "test114.zip"; 

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) 
{ 
    exit("cannot open <$filename>\n"); 
} 


$file="http://ichart.finance.yahoo.com/table.csv?s=YHOO&d=9&e=25&f=2012&g=d&a=0&b=3&c=2000&ignore=.csv"; 
$filedata = file_get_contents($file); 
$zip->addFromString("file1", $filedata); 
$zip->close(); 

$path = getcwd()."/".$filename; 

header("Content-Transfer-Encoding: Binary "); 
header('Content-Type: application/zip'); 
header("Content-Disposition: attachment; filename=$filename"); 
header('Content-Length: ' . filesize($filename)); 
readfile($path); 


unlink($filename); 

exit; 
?> 
相關問題