Skip to content

Instantly share code, notes, and snippets.

@zollinger
Created November 10, 2010 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zollinger/671322 to your computer and use it in GitHub Desktop.
Save zollinger/671322 to your computer and use it in GitHub Desktop.
Outputs contents of a directory in json
<?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