I'm trying to pull the names of the subdirectories of a directory into an array. I've tried a few different things. What am I doing wrong here?
I pulled this little function off of the php scandir() documentation page.
$data_dir = 'path/to/data_directory/';
function dirToArray( $data_dir ) {
$result = array();
$cdir = scandir( $data_dir ); // line 19
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($data_dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($data_dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$result[] = $value;
}
}
}
return $result;
}
$data_dir_array = dirToArray( $data_dir );
print_r( $data_dir_array );
ouput
Warning: scandir(/Users/alexcory/Library/Application\ Support/Alfred\ 2/Workflow\ Data): failed to open dir: No such file or directory in /Users/alexcory/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.36DE4754-2C41-4A38-BA8A-FF48D97C6371/open_data.php on line 19
Warning: scandir(): (errno 2): No such file or directory in /Users/alexcory/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.36DE4754-2C41-4A38-BA8A-FF48D97C6371/open_data.php on line 19
Warning: Invalid argument supplied for foreach() in /Users/alexcory/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.36DE4754-2C41-4A38-BA8A-FF48D97C6371/open_data.php on line 20 Array ( )