データベースに保存されているすべての画像の zip ファイルを作成しようとしています。これは、ユーザーがダウンロード ボタンをクリックしたときに実行されます。今、ダウンロード ボタンをクリックすると、zip ファイルが作成され、ファイルを開くか、コンピューターに保存するかを尋ねるウィンドウが表示されます。ファイルを開くと、すべてのファイル名が表示されます (想定どおり)。問題は、画像を表示しようとすると、名前が異なるだけですべて同じ画像になることです。
これが私のコードです:
/* creates a compressed zip file */
function create_zip($files = array(), $destination = '', $overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$path = 'admin/ReviewFiles/';
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
$fullFile = $path . $file;
if(file_exists($fullFile)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file);
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
And here's where the function is called:
$i = 0;
$reviewFiles = array();
while ($row = mysql_fetch_object($result))
{
$reviewFiles[$i] = $row->fileName;
$i++;
}
$zipCreated = create_zip($reviewFiles, 'reviewFiles.zip', true);
$file = 'reviewFiles.zip';
if ($zipCreated) {
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=reviewFiles.zip');
header('Expires: 0');
header('Pragma: no-cache');
readfile("reviewFiles.zip");
exit;
}
実際に各画像をダウンロードしていない理由を突き止めるために、考えられるすべてのことを試しました。したがって、明確に言えば、私の質問は次のとおりです。コードを修正して、名前が異なる同じ画像の1つの画像だけでなく、実際に各ファイルを圧縮するにはどうすればよいですか?
ありがとう!