Last active
January 18, 2023 20:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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