Php 从其他站点下载文件并添加到zip
有一些csv文件托管在另一个网站上,我正在尝试检索它们并打包成zip文件下载。拉链似乎起作用,但包装总是空的。非常感谢您的帮助。。。顺便说一下,这是用PHP编写的Php 从其他站点下载文件并添加到zip,php,zip,Php,Zip,有一些csv文件托管在另一个网站上,我正在尝试检索它们并打包成zip文件下载。拉链似乎起作用,但包装总是空的。非常感谢您的帮助。。。顺便说一下,这是用PHP编写的 <?php $zip = new ZipArchive(); $filename = "test114.zip"; $numParts = count($file_names); if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cann
<?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);
?>
尝试从此行中删除扩展名:
header("Content-Disposition: attachment; filename=\"".$filename."\";");
您已经在第二行(代码)上设置了文件扩展名。请尝试此操作,并确保您在当前工作目录中具有写入权限
<?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;
?>