<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
// Directory where images are stored - one folder back (site's public root)
$imageDirectory = '../images/';
// Supported image extensions
$supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
$images = [];
// Check if directory exists
if (!is_dir($imageDirectory)) {
// Try alternative paths
$alternativePaths = [
'../../images/',
'./images/',
'../../../images/',
$_SERVER['DOCUMENT_ROOT'] . '/images/'
];
$found = false;
foreach ($alternativePaths as $altPath) {
if (is_dir($altPath)) {
$imageDirectory = $altPath;
$found = true;
break;
}
}
if (!$found) {
echo json_encode([
'error' => 'Images directory not found',
'tried_paths' => array_merge(['../images/'], $alternativePaths),
'current_dir' => getcwd(),
'document_root' => $_SERVER['DOCUMENT_ROOT'] ?? 'not set'
]);
exit;
}
}
// Scan directory for image files
$files = scandir($imageDirectory);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $imageDirectory . $file;
// Check if it's a file (not a subdirectory)
if (is_file($filePath)) {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
// Check if it's a supported image format
if (in_array($extension, $supportedExtensions)) {
// Return the relative path for browser access
// Convert the server path to a web-accessible path
$webPath = str_replace('../', '', $imageDirectory) . $file;
$images[] = $webPath;
}
}
}
// Sort images alphabetically
sort($images);
echo json_encode($images);
?>