Skip to content

Instantly share code, notes, and snippets.

@X-Raym
Last active January 18, 2023 20:56
Show Gist options
  • Save X-Raym/d0930057cc298ce5655397a1082e8126 to your computer and use it in GitHub Desktop.
Save X-Raym/d0930057cc298ce5655397a1082e8126 to your computer and use it in GitHub Desktop.
List all images files names in PHP file directory and export to JSON
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$exts = ["png", "jpg"];
$dir = "./";
$list = [];
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) != false) {
if ($file != "." and $file != "..") {
$fileNameParts = explode(".", $file);
$ext = end($fileNameParts);
if (in_array($ext, $exts)) {
array_push($list, $file);
}
}
}
}
$return_array = ["files" => $list];
echo json_encode($return_array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment