Forum Moderators: coopster
function directoryListingCheck( $path = '.', $level = 0 )
{
$myusername=$_POST['myusername'];
$myusername=stripslashes($myusername); //define 'myusername
$myusername=mysql_real_escape_string($myusername); $lettermyusername = substr($myusername, 0,1); //get first character of string
$lettermyusername = stripslashes(htmlspecialchars($lettermyusername));
$ignore = array( '.', '..' ); // Directories to ignore when listing output.
$dh = @opendir( $path ); // Open the directory to the handle $dh
$i=0;
while( (false !== ( $file = readdir( $dh ) )) && ($i<=36)) // Loop through the directory
//$file=readdir($dh);
{
if( !in_array( $file, $ignore ) ) // Check that this file is not to be ignored
{
$spaces = str_repeat( ' ', ( $level * 5 ) ); // Indent spacing for better view
if(is_dir( "$path/$file" ) ) // Show directories only
{
// Re-call this same function but on a new directory.this is what makes function recursive.
$firstLetterDir=substr($file,0,1);
//echo $firstLetterDir;
if($lettermyusername == $firstLetterDir){
echo "match found";
mkdir("$DOCUMENT_ROOT/users/$lettermyusername/$myusername");
break;
}else{die("could not create the folder");}
}
}
$i++;
}
closedir( $dh ); // Close the directory handle
}
What I want the function to do is to match the input username's first character to the corresponding first letter in the alphabet(lower case). The script works fine, but somehow I am not using the mkdir correctly to create the corresponding directory.
Example to better explain:
('users' in the path name above contains folders named a-z lower case)
1.) user inputs --> john (username)
2.) function checks folder name and matches it to 'j' in username first character
3.) mkdir creates path/to/users/j/john,
What am I doing wrong? Any help would be greatly appreciated! Thanks.
function directoryListingCheck( $path = '.', $level = 0 )
{
$myusername=$_POST['myusername'];
$myusername=stripslashes($myusername); //define 'myusername
$myusername=mysql_real_escape_string($myusername);
$lettermyusername = substr($myusername, 0,1); //get first character of string
$lettermyusername = stripslashes(htmlspecialchars($lettermyusername)); $ignore = array( '.', '..' ); // Directories to ignore when listing output.
$dh = @opendir( $path ); // Open the directory to the handle $dh
while( (false !== ( $file = readdir( $dh ) ))) // Loop through the directory
{
if( !in_array( $file, $ignore ) ) // Check that this file is not to be ignored
{
$spaces = str_repeat( ' ', ( $level * 5 ) ); // Indent spacing for better view
if(is_dir( "$path/$file" ) ) // Show directories only
{
//get first letter of directory
$firstLetterDir=substr($file,0,1);
if($lettermyusername == $firstLetterDir){
mkdir("../../example/path/$lettermyusername/$myusername");
}
}
}
}
closedir( $dh ); // Close the directory handle
}