Created
April 17, 2017 15:59
-
-
Save timbophillips/d9f42ab3e9ba3d59ae0fd193ecccd97c to your computer and use it in GitHub Desktop.
php script to return files in folder as JSON from web service
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 | |
/* | |
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