Skip to content

Instantly share code, notes, and snippets.

@fugudesign
Last active February 13, 2018 11:19
Show Gist options
  • Save fugudesign/bf88606c114f75d509528974488f05e4 to your computer and use it in GitHub Desktop.
Save fugudesign/bf88606c114f75d509528974488f05e4 to your computer and use it in GitHub Desktop.
Simple images API PHP
<?php
/*
JSON list of all images files in current directory
*/
header("Access-Control-Allow-Origin: *");
$dir = ".";
$dh = opendir($dir);
$image_files = array();
while (($file = readdir($dh)) !== false) {
// Exclude retina versions from listing
if (preg_match("/[^@]*(?!.*@2x)\.(jpg|png|gif|jpeg)/A", $file)) {
$image_files []= $_SERVER['SCRIPT_URI'].'/'.$file;
}
}
// Return response as JSON
echo json_encode($image_files);
closedir($dh);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment