3

別の Web サイトでホストされているいくつかの 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."\";");

ファイル名拡張子は、(コードの) 2 行目に既に設定されています。

于 2012-10-30T06:49:02.027 に答える