Skip to content

Instantly share code, notes, and snippets.

@timbophillips
Created April 17, 2017 15:59
Show Gist options
  • Save timbophillips/d9f42ab3e9ba3d59ae0fd193ecccd97c to your computer and use it in GitHub Desktop.
Save timbophillips/d9f42ab3e9ba3d59ae0fd193ecccd97c to your computer and use it in GitHub Desktop.
php script to return files in folder as JSON from web service
<?php
/*
call this with GET
returns JSON of files in folder
optional arguments:
folder (defaults to current folder)
extension (defaults to all files)
*/
if (isset($_GET['folder'])) {
$folder = $_GET['folder'];
$thelot = scandir($folder);
}
else {
$folder = './';
$thelot = scandir($folder);
}
if (isset($_GET['extension'])) {
$extension = $_GET['extension'];
$justfiles = array_filter($thelot, function($item) use ($extension) {
return !is_dir($item) and pathinfo($item, PATHINFO_EXTENSION) == $extension;
});
}
else {
$justfiles = array_filter($thelot, function($item) {
return !is_dir($item);
});
}
echo json_encode($justfiles);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment