Created
November 10, 2010 19:00
-
-
Save zollinger/671322 to your computer and use it in GitHub Desktop.
Outputs contents of a directory in json
This file contains hidden or 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 | |
/** | |
* GET Params: | |
* dir => dir name | |
* abspath => true/false | |
*/ | |
header('Content-type: application/json'); | |
$defaults = array( | |
'dir'=>'data', | |
'abspath'=>true | |
); | |
$options = array_merge($defaults, $_GET); | |
$files = array(); | |
$options['dir'] = preg_replace('/\.+/i', '', $options['dir']); | |
if( $options['abspath'] == true){ | |
$tmp = explode('?', $_SERVER['REQUEST_URI'], 2); | |
$prefix = 'http://'.$_SERVER['HTTP_HOST'] .'/'. basename($tmp[0]).'/'; | |
}else{ | |
$prefix = ''; | |
} | |
if ($handle = opendir( dirname(__FILE__). '/'. $options['dir'])) { | |
while (false !== ($file = readdir($handle))) { | |
if($file != '.' && $file != '..' && is_file($options['dir'].'/'.$file)){ | |
$files[] = $prefix.$options['dir'].'/'.$file; | |
} | |
} | |
closedir($handle); | |
} | |
echo json_encode($files); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment