0 レビュー
1 回答
codeigniter-PHPでzipアーカイブを作成できますが、正しくダウンロードできません
CodeIgniterベースのWebアプリに取り組んでおり、サードパーティに渡すために管理者に車両画像のzipファイルをダウンロードする機能を実装しています。
zipアーカイブを問題なく作成できます。後の部分をコメントアウトすると、zipファイルが作成され、正しいファイルが含まれていることがわかります。通常どおりに抽出できます。
ただし、作成後にユーザーがファイルをダウンロードできるようにするのは難しいことです。私が実装したものでは、正しい名前のファイルをダウンロードできますが、解凍すると次のメッセージが表示されてエラーが発生します。
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
どこが間違っているのか考えてみませんか? Webで複数のチュートリアルを確認しましたが、問題がどこにあるのかわかりませんでした。
public function downloadvehicleimages()
{
// If logged in...
if ($this->adminuser)
{
// Get data
$usedcars = $this->adminmodel->getUsedCarsWithLimit();
// Get the registrations for these vehicles
$registrations = array();
foreach($usedcars->result_array() as $usedcar)
{
array_push($registrations, $usedcar['Registration']);
}
// Get all the images for these registrations
$images = array();
$original_dir = getcwd();
chdir('../used-cars/static/images/custom/');
foreach($registrations as $registration)
{
$vehicle_images = glob(strtoupper($registration) . '_*.jpg');
sort($vehicle_images);
foreach($vehicle_images as $vehicle_image)
{
array_push($images, $vehicle_image);
}
}
// Zip them up
$zipname = 'images.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach($images as $image)
{
$zip->addFile($image);
}
$zip->close();
// Let user download the file
$this->output->set_header('Content-Type: application/zip');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header('Expires: 0');
$this->output->set_header("Content-Disposition: attachment;filename='$zipname'");
$this->output->set_header('Content:Length: ' . filesize($zipname));
unlink($zipname);
chdir($original_dir);
}
}
わからない
0
レビュー
答え :
解決策:
fopen()を使用してzipファイルのコンテンツを読み取り、readfile()を使用してファイルのコンテンツを訪問者に渡す必要があります。また、完全なシステムディレクトリURIをファイルの場所に提供します:
$filename = 'images.zip';
$path = /var/www/yourdomain.com/images.zip';
if(!file_exists($path))
{
die('Error: file not found');
}
else {
$handle = fopen($path, "r");
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
flush();
readfile($path);
fclose($handle);
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。