/
home
/
efamember
/
domains
/
efa-member.com
/
public_html
/
backoffice
/
ajax
/
company_info
/
up file
home
<?php header('Content-Type: application/json'); session_start(); include('../../../config/main_function.php'); $secure = "cAh3DrJACzw4RbU"; $connection = connectDB($secure); $company_id = $_POST['company_id']; $sql = "SELECT * FROM `tbl_company_file` WHERE company_id = '$company_id' ORDER BY list_order"; $rs = mysqli_query($connection, $sql) or die($connection->error); $sql_comp_info = "SELECT * FROM tbl_company_info WHERE company_id = '$company_id'"; $res_comp_info = mysqli_query($connection, $sql_comp_info); $row_comp_info = mysqli_fetch_assoc($res_comp_info); $imageUrls = []; $imageUrls[] = "https://efa.or.th/upload/company_files/" . $row_comp_info['company_profile_image']; while ($row = mysqli_fetch_array($rs)) { $imageUrls[] = "https://efa.or.th/upload/company_files/" . $row['file_name']; } // โฟลเดอร์ชั่วคราวและ ZIP $tempDir = 'temp_images/'; $zipDir = 'downloads/'; $zipName = 'images_' . $row_comp_info['company_name'] . '.zip'; $zipPath = $zipDir . $zipName; // เตรียมโฟลเดอร์ if (!is_dir($tempDir)) mkdir($tempDir, 0777, true); if (!is_dir($zipDir)) mkdir($zipDir, 0777, true); // ล้างไฟล์เก่า foreach (glob($tempDir . '*') as $file) { if (is_file($file)) unlink($file); } foreach (glob($zipDir . '*') as $file) { if (is_file($file)) unlink($file); } // ดาวน์โหลดรูป $downloadedFiles = []; foreach ($imageUrls as $imageUrl) { $imageName = basename(parse_url($imageUrl, PHP_URL_PATH)); $localPath = $tempDir . $imageName; $imageData = @file_get_contents($imageUrl); if ($imageData !== false) { file_put_contents($localPath, $imageData); $downloadedFiles[] = $localPath; } } // สร้าง ZIP $zip = new ZipArchive(); if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) { foreach ($downloadedFiles as $file) { $zip->addFile($file, basename($file)); } $zip->close(); } else { echo json_encode(['rs' => 0, 'msg' => 'สร้างไฟล์ ZIP ไม่สำเร็จ']); exit; } // ลบไฟล์ภาพชั่วคราว foreach ($downloadedFiles as $file) { unlink($file); } // ส่งชื่อ ZIP กลับ echo json_encode([ 'rs' => 1, 'file' => $zipPath ]);