3

在另一个网站上托管了一些 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);

?>
4

2 回答 2

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;
?>
于 2012-10-30T07:24:42.113 回答
0

尝试从此行中删除扩展名:

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

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

于 2012-10-30T06:49:02.027 回答