File: /var/www/content-sp/work-sp/all_images_on_server.php
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
include_once 'config.php';
session_start();
if (!empty($_SESSION['editor'])) {
$folders = [
'contentsp_photos',
'tet_admin_photos',
'tet_admin_photos_oly',
'tet_admin_photos_sp'
];
$selected_folder = $_GET['folder'] ?? '';
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$per_page = 100;
// ==== Optimized function to fetch images with pagination ====
function get_images($dir, $page, $per_page)
{
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
$images = [];
$server_dir = '/var/www/content-sp/uploads/' . $dir;
$base_url = 'https://content.grapossconnect.com/uploads/' . $dir;
if (!is_dir($server_dir)) return [];
// Calculate offset
$offset = ($page - 1) * $per_page;
// List only required chunk of files sorted by modified date DESC
$cmd = "ls -t " . escapeshellarg($server_dir) . " | tail -n +" . ($offset + 1) . " | head -n $per_page";
$files = shell_exec($cmd);
$files = array_filter(explode("\n", trim($files)));
foreach ($files as $file) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, $allowed_ext)) {
$file_path = rtrim($server_dir, '/') . '/' . ltrim($file, '/');
if (is_file($file_path)) {
$modified = filemtime($file_path);
$images[] = [
'url' => rtrim($base_url, '/') . '/' . ltrim($file, '/'),
'name' => $file,
'modified' => date('d M Y, h:i A', $modified)
];
}
}
}
return $images;
}
// ==== Get total image count efficiently ====
function get_total_images($dir)
{
$server_dir = '/var/www/content-sp/uploads/' . $dir;
if (!is_dir($server_dir)) return 0;
$count = shell_exec("ls " . escapeshellarg($server_dir) . " | wc -l");
return (int)trim($count);
}
$total_images = 0;
$images = [];
if ($selected_folder && in_array($selected_folder, $folders)) {
$total_images = get_total_images($selected_folder);
$images = get_images($selected_folder, $page, $per_page);
}
$total_pages = ceil($total_images / $per_page);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Viewer</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light p-3">
<div class="container">
<form class="row g-3 mb-4" method="get">
<div class="col-md-3">
<h4 class="mb-4">View Uploaded Images</h4>
</div>
<div class="col-md-3">
<select name="folder" class="form-select">
<option value="">-- Select Folder --</option>
<?php foreach ($folders as $f): ?>
<option value="<?= htmlspecialchars($f) ?>" <?= $selected_folder === $f ? 'selected' : '' ?>>
<?= htmlspecialchars($f) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary w-100">View Images</button>
</div>
</form>
<?php if ($selected_folder): ?>
<table class="table table-bordered align-middle">
<tr>
<td>Showing images from: <span class="text-primary"><?= htmlspecialchars($selected_folder) ?></span></td>
<td>Total Images: <strong><?= $total_images ?></strong></td>
<td>Folder Path: <span class="text-danger"><?php echo 'https://content.grapossconnect.com/uploads/' . $selected_folder . '/'; ?><span></td>
</tr>
</table>
<?php if ($images): ?>
<table class="table table-bordered align-middle text-center">
<tr class="table-secondary">
<th>#</th>
<th>Filename</th>
<th>Last Modified</th>
<th>Image</th>
</tr>
<?php $count = ($page - 1) * $per_page + 1; ?>
<?php foreach ($images as $img): ?>
<tr>
<td><?= $count++ ?></td>
<td><?= htmlspecialchars($img['name']) ?></td>
<td><?= htmlspecialchars($img['modified']) ?></td>
<td>
<img src="<?= htmlspecialchars($img['url']) ?>"
class="img-thumbnail"
style="width:100px; cursor:pointer;"
data-src="<?= htmlspecialchars($img['url']) ?>"
data-modified="<?= htmlspecialchars($img['modified']) ?>"
onclick="showImage(this)">
</td>
</tr>
<?php endforeach; ?>
</table>
<nav>
<ul class="pagination justify-content-center" style="display: ruby;">
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
<a class="page-link" href="?folder=<?= urlencode($selected_folder) ?>&page=<?= $i ?>" style="width: 50px;">
<?= $i ?>
</a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php else: ?>
<div class="alert alert-warning">No images found in this folder.</div>
<?php endif; ?>
<?php endif; ?>
</div>
<!-- Image Modal -->
<div class="modal fade" id="imageModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Image Viewer</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center">
<img id="modalImg" src="" class="img-fluid mb-3" alt="Full Image">
<p id="imgInfo" class="text-muted small"></p>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const modalEl = new bootstrap.Modal(document.getElementById('imageModal'));
const modalImg = document.getElementById('modalImg');
const imgInfo = document.getElementById('imgInfo');
function showImage(el) {
const src = el.dataset.src;
const modified = el.dataset.modified;
modalImg.src = src;
imgInfo.textContent = 'Loading...';
modalEl.show();
modalImg.onload = function() {
imgInfo.textContent = `Actual Size: ${this.naturalWidth} × ${this.naturalHeight}px | Last Modified: ${modified}`;
};
}
</script>
</body>
</html>
<?php } else { echo "login first"; } ?>