Page 1 of 1

can't readdir() folders with apostrophe or comma in the name

Posted: Thu Jun 19, 2008 12:57 pm
by toonskies
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;
}

Re: can't readdir() folders with apostrophe or comma in the name

Posted: Thu Jun 19, 2008 1:00 pm
by Christopher
You probably need to escape the quotes ... not sure about the commas.

Re: can't readdir() folders with apostrophe or comma in the name

Posted: Thu Jun 19, 2008 1:57 pm
by toonskies
I thought of that, but i'm not sure how to do it. I've tried replacing the ' with \' and with \00100111 (UTF-8 (binary))
but to no avail. I'm not sure at which point the function fails to read from a folder with a ' or ,
for example i've changed:

for($i=0; $i < sizeof($temp); $i++)
{
$temp2 = explode('|', $temp[$i]);
$artFolders[$i] = $temp2[1];
}

to:

for($i=0; $i < sizeof($temp); $i++)
{
$temp2 = explode('|', $temp[$i]);
$artFolders[$i] = str_replace('\'', \00100111, $temp2[1]);
}

so that the opendir() and / or readdir() method will interpret the ' as it's binary equivalent, but it doesn't work.
I'm kind of shooting in the dark trying to find the problem / solution at the moment, as i'm not a php expert.

Re: can't readdir() folders with apostrophe or comma in the name

Posted: Thu Jun 19, 2008 2:32 pm
by toonskies
Actually, this is more accurate and would replace ' with \'

for($i=0; $i < sizeof($temp); $i++)
{
$temp2 = explode('|', $temp[$i]);
$artFolders[$i] = str_replace('\'', "\u005C"."\u0027", $temp2[1]);
}

but it still doesn't work.