Forum Moderators: coopster

Message Too Old, No Replies

help using the mkdir in php5 to create a directory

mkdir help

         

rodriguez1804

10:57 pm on Jul 23, 2008 (gmt 0)

10+ Year Member



Ok, I seem to be having loads of trouble getting the mkdir function to work inside a function. Particularly, this following function:


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( '&nbsp;', ( $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.

rodriguez1804

11:06 pm on Jul 23, 2008 (gmt 0)

10+ Year Member



Ok, so I found my first mistake (using == instead of = for char values), but it 's still not creating the directory. I ll keep working on it.

rodriguez1804

11:16 pm on Jul 23, 2008 (gmt 0)

10+ Year Member



whohoo! Got it working! Thanks anyways guys. Later!

janharders

11:27 pm on Jul 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



glad to be of service ;)

haven't read your original post -- if it wasn't just a typo, mind sharing the final solution in case someone else runs into the same problem?

rodriguez1804

7:15 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



Just in case anybody is having a similar problem, here is the final version that I was able to get working for me. Cheers!

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( '&nbsp;', ( $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
}