Joywork/ajax/getPhotoArchive.php

82 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2026-05-22 20:21:54 +02:00
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
if ($_SESSION['id'] && $_GET['id'] && is_numeric($_GET['id'])) {
$sql = "SELECT * FROM objects WHERE objects.id = $_GET[id]";
$rez = mysql_query($sql);
$obj = mysql_fetch_assoc($rez);
if ($obj) {
$sqlPhotoInfo = "SELECT oop.sort_order, p.photo FROM objects_object_photo oop, object_photo p WHERE p.id = oop.object_photo_id AND oop.object_id = $obj[id] ORDER BY oop.sort_order";
$rezPhotoInfo = mysql_query($sqlPhotoInfo);
if (mysql_num_rows($rezPhotoInfo) > 0) {
while ($photoInfo = mysql_fetch_assoc($rezPhotoInfo)) {
$obj['photo' . $photoInfo['sort_order']] = $photoInfo['photo'];
}
}
$zip = new ZipArchive(); // Load zip library
if ($obj['adres']) {
$obj['adres'] = str_replace(".", "", $obj['adres']);
$obj['adres'] = str_replace(",", "", $obj['adres']);
$obj['adres'] = str_replace("/", "", $obj['adres']);
$address_short = mb_substr($obj['adres'], 0, 50);
$zip_name = trim($address_short)."_".$obj['id'].".zip";
} else {
$zip_name = $obj['id']."_".time() . ".zip"; // Zip name
}
if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) { // Opening zip file to load files
echo 'Sorry ZIP creation failed at this time';
}
$ph = 1;
for ($i = 0; $i <= 40; $i++) {
$path = $obj['photo' . $i];
if (stripos($obj['photo' . $i], "http://") === false && stripos($obj['photo' . $i], "https://") === false) {
$obj['photo' . $i] = $_SERVER['DOCUMENT_ROOT'] . "/photos/" . $obj['photo' . $i];
}
if ($path and @fopen($obj['photo' . $i], "r")) {
$f1 = file_get_contents($obj['photo' . $i]);
if (stripos($f1, "<html") === false) {
$filename = $obj['photo' . $i];
$lastPos = strrpos($filename, '.');
if ($lastPos) {
$filename = substr($filename, $lastPos);
}
if (mb_strlen($filename) > 5) {
$filename = ".jpg";
}
$zip->addFromString($ph.$filename, $f1);
$ph++;
}
}
}
$zip->close();
if (file_exists($zip_name)) {
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_name . '"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
exit();
}
}
}