| 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:/bpkd_/admin/ |
Upload File : |
<?php
$page_title = 'Manajemen Media';
require_once 'config/database.php';
require_once 'includes/header.php';
// Handle upload
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$target_dir = "../uploads/media/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$filename = basename($_FILES['file']['name']);
$file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$new_filename = time() . '_' . preg_replace('/[^a-zA-Z0-9.-]/', '_', $filename);
$target_file = $target_dir . $new_filename;
$filetype = $_FILES['file']['type'];
$filesize = $_FILES['file']['size'];
// Allow certain file formats
$allowed_types = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'mp4', 'webm'];
if (in_array($file_ext, $allowed_types)) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
$title = sanitize($_POST['title'] ?? $filename);
$alt_text = sanitize($_POST['alt_text'] ?? '');
$description = sanitize($_POST['description'] ?? '');
$uploaded_by = $_SESSION['user_id'];
$filepath = 'uploads/media/' . $new_filename;
$conn->query("INSERT INTO media (filename, filepath, filetype, filesize, title, alt_text, description, uploaded_by)
VALUES ('$filename', '$filepath', '$filetype', $filesize, '$title', '$alt_text', '$description', $uploaded_by)");
$success = "File berhasil diupload!";
} else {
$error = "Gagal mengupload file.";
}
} else {
$error = "Tipe file tidak diizinkan.";
}
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
$media = $conn->query("SELECT filepath FROM media WHERE id = $id")->fetch_assoc();
if ($media) {
$file_path = "../" . $media['filepath'];
if (file_exists($file_path)) {
unlink($file_path);
}
$conn->query("DELETE FROM media WHERE id = $id");
}
redirect('media.php?msg=deleted');
}
// Get all media
$media_files = $conn->query("SELECT m.*, u.full_name as uploader FROM media m LEFT JOIN users u ON m.uploaded_by = u.id ORDER BY m.created_at DESC");
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">File berhasil dihapus!</div>';
}
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Manajemen Media</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadModal">
<i class="fas fa-upload me-2"></i>Upload File
</button>
</div>
<?php if (isset($success)): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<?php echo $msg; ?>
<!-- Upload Modal -->
<div class="modal fade" id="uploadModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" enctype="multipart/form-data">
<div class="modal-header">
<h5 class="modal-title">Upload File Baru</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Pilih File <span class="text-danger">*</span></label>
<input type="file" class="form-control" name="file" required>
<small class="text-muted">Format: JPG, PNG, GIF, PDF, DOC, XLS, MP4. Maks 10MB</small>
</div>
<div class="mb-3">
<label class="form-label">Judul</label>
<input type="text" class="form-control" name="title">
</div>
<div class="mb-3">
<label class="form-label">Alt Text (untuk gambar)</label>
<input type="text" class="form-control" name="alt_text">
</div>
<div class="mb-3">
<label class="form-label">Deskripsi</label>
<textarea class="form-control" name="description" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
</div>
<!-- Media Grid -->
<div class="row">
<?php if ($media_files->num_rows > 0): ?>
<?php while($media = $media_files->fetch_assoc()):
$file_ext = strtolower(pathinfo($media['filename'], PATHINFO_EXTENSION));
$is_image = in_array($file_ext, ['jpg', 'jpeg', 'png', 'gif']);
?>
<div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 mb-4">
<div class="card media-card h-100">
<div class="media-preview">
<?php if ($is_image): ?>
<img src="../<?php echo $media['filepath']; ?>" class="card-img-top" alt="<?php echo $media['title']; ?>">
<?php else: ?>
<div class="file-icon text-center p-4">
<?php if ($file_ext == 'pdf'): ?>
<i class="fas fa-file-pdf fa-4x text-danger"></i>
<?php elseif (in_array($file_ext, ['doc', 'docx'])): ?>
<i class="fas fa-file-word fa-4x text-primary"></i>
<?php elseif (in_array($file_ext, ['xls', 'xlsx'])): ?>
<i class="fas fa-file-excel fa-4x text-success"></i>
<?php elseif (in_array($file_ext, ['mp4', 'webm'])): ?>
<i class="fas fa-file-video fa-4x text-info"></i>
<?php else: ?>
<i class="fas fa-file fa-4x text-secondary"></i>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="card-body p-2">
<h6 class="card-title text-truncate" title="<?php echo $media['title']; ?>">
<?php echo $media['title'] ?: $media['filename']; ?>
</h6>
<p class="small text-muted mb-0">
<?php echo strtoupper($file_ext); ?> •
<?php echo round($media['filesize'] / 1024, 1); ?> KB
</p>
</div>
<div class="card-footer bg-transparent p-2">
<div class="btn-group w-100">
<button type="button" class="btn btn-sm btn-info" onclick="copyToClipboard('../<?php echo $media['filepath']; ?>')">
<i class="fas fa-link"></i>
</button>
<a href="../<?php echo $media['filepath']; ?>" target="_blank" class="btn btn-sm btn-primary">
<i class="fas fa-eye"></i>
</a>
<a href="?delete=<?php echo $media['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
<i class="fas fa-trash"></i>
</a>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class="col-12">
<div class="text-center py-5">
<i class="fas fa-images fa-4x text-muted mb-3"></i>
<h5>Belum ada media</h5>
<p class="text-muted">Klik tombol Upload untuk menambahkan file</p>
</div>
</div>
<?php endif; ?>
</div>
</div>
<script>
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
alert('URL file berhasil disalin!');
});
}
</script>
<style>
.media-card {
transition: all 0.3s;
border: 1px solid #e3e6f0;
cursor: pointer;
}
.media-card:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.media-preview {
height: 150px;
overflow: hidden;
background: #f8f9fc;
display: flex;
align-items: center;
justify-content: center;
}
.media-preview img {
width: 100%;
height: 100%;
object-fit: cover;
}
.file-icon {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<?php require_once 'includes/footer.php'; ?>