从其他站点下载文件并添加到zip中

时间:2012-10-30 06:22:16

标签: php zip

在另一个网站上托管了一些csv文件我正在尝试检索它们并将其打包成一个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);

?>

2 个答案:

答案 0 :(得分:0)

尝试从此行中删除扩展程序:

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

您已经在第二行(代码)设置了文件扩展名。

答案 1 :(得分: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;
?>
相关问题