can't readdir() folders with apostrophe or comma in the name
Posted: Thu Jun 19, 2008 12:57 pm
Hello,
I'm using the opendir() readdir() methods to read directories and sub directories and files.
Everything works fine, as in I can read sub folders of a folder and all subsequent files in all the sub folders,
However it doesn't seem to read from folders with apostrophes ( ' ) or commas ( , ) in them. It just ignores those
folders and goes onto the next one. This happens at all levels of the folder tree, whether it's the root folder or 2 levels deep. Spaces aren't a problem.
Below is a snippet of the code i've written. it's a function where I pass in the root (music) folder along with
$art_str (a delimited string containing all the bands with their id's in the music folder), which is converted into an array in the function:
----------------------------------------------------------------
function getAlbumFolders($root, $art_str) {
$string = "";
$temp = explode('~', $art_str);
$artFolders = array();
for($i=0; $i < sizeof($temp); $i++)
{
$temp2 = explode('|', $temp[$i]);
$artFolders[$i] = $temp2[1];
}
for($i=0; $i < sizeof($artFolders); $i++)
{
$alb_Count = 0;
$path = $root.'/'.$artFolders[$i]; // i.e.: "band_name/album 3, blah blah"
$dir = opendir($path);
while ($folder = readdir($dir))
{
if (is_dir($path.'/'.$folder) && ! is_file($folder) && $folder != "." && $folder != "..")
{
$string.= ($alb_Count+1).'|'; // id
$string.= $folder.'|'; // album_name
$string.= ($i+1).'~'; // artist_id
$alb_Count++;
}
}
closedir($dir);
}
$string = substr($string, 0, -1); // get rid of the last asterix at the end of the string
return $string;
}
I'm using the opendir() readdir() methods to read directories and sub directories and files.
Everything works fine, as in I can read sub folders of a folder and all subsequent files in all the sub folders,
However it doesn't seem to read from folders with apostrophes ( ' ) or commas ( , ) in them. It just ignores those
folders and goes onto the next one. This happens at all levels of the folder tree, whether it's the root folder or 2 levels deep. Spaces aren't a problem.
Below is a snippet of the code i've written. it's a function where I pass in the root (music) folder along with
$art_str (a delimited string containing all the bands with their id's in the music folder), which is converted into an array in the function:
----------------------------------------------------------------
function getAlbumFolders($root, $art_str) {
$string = "";
$temp = explode('~', $art_str);
$artFolders = array();
for($i=0; $i < sizeof($temp); $i++)
{
$temp2 = explode('|', $temp[$i]);
$artFolders[$i] = $temp2[1];
}
for($i=0; $i < sizeof($artFolders); $i++)
{
$alb_Count = 0;
$path = $root.'/'.$artFolders[$i]; // i.e.: "band_name/album 3, blah blah"
$dir = opendir($path);
while ($folder = readdir($dir))
{
if (is_dir($path.'/'.$folder) && ! is_file($folder) && $folder != "." && $folder != "..")
{
$string.= ($alb_Count+1).'|'; // id
$string.= $folder.'|'; // album_name
$string.= ($i+1).'~'; // artist_id
$alb_Count++;
}
}
closedir($dir);
}
$string = substr($string, 0, -1); // get rid of the last asterix at the end of the string
return $string;
}