Page 1 of 1
can't use opendir() and readdir() on folders with ' or ,
Posted: Fri Jun 20, 2008 10:11 am
by toonskies
Anyone know how to succesfully use opendir() and readdir() on folders with apostrophes ( ' ) and / or commas ( , )
in the folder name?
I've tried to escape the apostrophes with addslashes() and with str_replace('\'', '\\\'' $str) but with no success.
I still can't read any folder names with apostrophes or commas in the folder name. every folders without these is read fine.
any ideas?
Re: can't use opendir() and readdir() on folders with ' or ,
Posted: Fri Jun 20, 2008 1:08 pm
by phice
What happens (what displays for the folder names) when you use readdir on the parent directory that contains the folder with the quotes/commas?
Also, can you include the part of your code so I can take a look at it?
Re: can't use opendir() and readdir() on folders with ' or ,
Posted: Sat Jun 21, 2008 6:50 am
by toonskies
I'm not sure how it reads it, or even if it CAN read it, as it's read withing a function and stored an array. All I know is that any folder with apostrophes or commas in them are skipped. for instance, in the 'music' folder, there is an artist folder called: Ba'nd. (i put in the apostrophe to test it). then say the second band is: band2. all album folders in Ba'nd are skipped but the album folder in band2 are successfully read and all songs within those albums are successfully read.
also, if there is an album folder in band2 with an apostrophe or comma, then that album is skipped and all it's songs.
I have 3 main functions:
getArtistFolders($root) where $root is the name of the music folder to read from. this function froms a delimited string containing all band names and their id's. (i seperate band / id groups with ~ and seperate band from id with | ). this string is then returned.
getAlbumFolders($root, $art_str) where $root is again the name of the music folder to read from and $art_str is the delimited string that was returned from the getArtistFolders($root) function. This function reads from all the artist folders, getting all the albums from every artist and forming a delimited string in a similar fashion to the previous function. this string also includes album id and artist id. This seems to be where the trouble lies. It can't seem to read from folders with ' or , in the folder name. although the problem might originate in the first function, in that the artist folder name with ' or , in it isn't even read properly and hence not included in the artist string.
The replace($str) is necessary, as without it, i get errors when trying to pass the strings back to the calling html page (because of the strings containing apostrophes).
However, this function is only called after the reading of the folders takes place, so it doesn't affect the name of the folders while they are being read.
the 3rd function: getSongs($root, $art_str, $alb_str) get's all the songs from all album folders from all artists. this function reads all song names successfully, including ones with ' and , in the name, which shows that the problem is only with folder names. I've pasted the entire php file code below. at the end, the artist, album & song string are returned to a html page where they are turned into arrays and rendered into a mySQL script which is used to create a database.
<?php
error_reporting(E_ALL);
function getArtistFolders($root) {
$string = "";
$Count = 0;
$dir = opendir($root);
while ($folder = readdir($dir))
{
if (is_dir($root.'/'.$folder) && ! is_file($folder) && $folder != "." && $folder != "..")
{
$string.= ($Count+1).'|'; // id
$string.= $folder.'~'; //artist_name
$Count++;
}
}
closedir($dir);
$string = substr($string, 0, -1); // get rid of the last asterix at the end of the string
return $string;
}
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];
$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;
}
function getSongs($root, $art_str, $alb_str) {
$string = "";
$art_temp = explode('~', $art_str);
$alb_temp = explode('~', $alb_str);
for($i=0; $i < sizeof($art_temp); $i++)
{
$temp = $art_temp[$i];
$art = explode("|", $temp);
$artists_array[$i][0] = $art[0];
$artists_array[$i][1] = $art[1];
}
for($i=0; $i < sizeof($alb_temp); $i++)
{
$temp = $alb_temp[$i];
$alb = explode("|", $temp);
$albums_array[$i][0] = $alb[0];
$albums_array[$i][1] = $alb[1];
$albums_array[$i][2] = $alb[2];
}
for($i=0; $i < sizeof($artists_array); $i++)
{
$filePath_1 = $root.'/'.$artists_array[$i][1].'/';
for($j=0; $j < sizeof($albums_array); $j++)
{
$songCount = 0;
$filePath_2 = $filePath_1.$albums_array[$j][1];
$dir = opendir($filePath_2);
while ($file = readdir($dir))
{
if (! is_dir($filePath_2.'/'.$file) && $file != "." && $file != ".." && eregi('.mp3', $file))
{
$string.=($songs_array[$songCount][0] = $songCount+1).'|'; // id
$string.=($songs_array[$songCount][1] = $filePath_2.'/'.$file).'|'; // song_path
$string.=($songs_array[$songCount][2] = str_replace('_', ' ', substr($file, 3, -4))).'|'; // song_name
$string.=($songs_array[$songCount][3] = $albums_array[$j][0]).'|'; // album_id
$string.=($songs_array[$songCount][4] = $artists_array[$i][0]).'~'; // artist_id
$songCount++;
}
}
closedir($dir);
}
}
$string = substr($string, 0, -1); // get rid of the last asterix at the end of the string
return $string;
}
function replace($str) {
$swap = array( array('\'', ','),
array("APOSTROPHE", "COMMA") );
for($i=0; $i < sizeof($swap[0]); $i++) {
$str = str_replace($swap[0][$i], $swap[1][$i], $str);
}
return $str;
}
$artists_string = replace(getArtistFolders($_GET['root_folder']));
$albums_string = replace(getAlbumFolders($_GET['root_folder'], $artists_string));
$songs_string = replace(getSongs($_GET['root_folder'], $artists_string, $albums_string));
echo "<script language=\"JavaScript\">\n";
echo "var artists = '<?php echo $artists_string; ?>';\n";
echo "var albums = '<?php echo $albums_string; ?>';\n";
echo "var songs = '<?php echo $songs_string; ?>';\n";
echo "window.parent.handleResponse_2(artists,albums,songs);\n";
echo "</script>\n";
?>
Re: can't use opendir() and readdir() on folders with ' or ,
Posted: Wed Jun 25, 2008 2:45 pm
by toonskies
Found the problem. After replacing , and ' with "COMMA" and "APOSTROPHE", i was then trying to read from folders called e.g.: "BanAPOSTROPHEd one" instead of Ban'd one.
Just did a reverse replace and everything's fine.