| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/laragon4/www/bpkd.test_/ |
Upload File : |
<?php
/**
* File: download.php
* Proses download file dan update download count
* Letakkan di root folder CMS
*/
// Load inisialisasi
require_once 'includes/init.php';
// Ambil ID laporan
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
$_SESSION['download_error'] = 'ID laporan tidak valid.';
header("Location: laporan.php");
exit;
}
// Ambil data laporan dari database
$query = "SELECT r.*, m.filepath, m.filename
FROM reports r
LEFT JOIN media m ON r.file_id = m.id
WHERE r.id = $id AND r.status = 'active'";
$result = $conn->query($query);
if (!$result || $result->num_rows == 0) {
$_SESSION['download_error'] = 'Data laporan tidak ditemukan.';
header("Location: laporan.php");
exit;
}
$report = $result->fetch_assoc();
// Validasi filepath
if (empty($report['filepath'])) {
$_SESSION['download_error'] = 'File tidak ditemukan. Silakan hubungi administrator.';
header("Location: laporan.php");
exit;
}
// Path lengkap file
$file_path = dirname(__FILE__) . '/' . $report['filepath'];
// Cek apakah file ada secara fisik
if (!file_exists($file_path)) {
$_SESSION['download_error'] = 'File tidak ditemukan. Silakan hubungi administrator.';
header("Location: laporan.php");
exit;
}
// Update download count
$conn->query("UPDATE reports SET download_count = download_count + 1 WHERE id = $id");
// Set headers untuk force download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($report['filename']) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
// Bersihkan output buffer
ob_clean();
flush();
// Baca file dan kirim ke browser
readfile($file_path);
exit;
?>