Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help automatically creating new user subdirectory

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
0
0
US
i was wondering if someone could assist me in creating the functionality where a person can sign into a website as a new user and the website automatically create a subdirectory named whatever they chose as a username?
 
Oops. Should be:
Code:
mkdir('./' . $username . '/', 0777);

Sorry about that.
 
Remember to check if the directory already excists!

is_dir -- Tells whether the filename is a directory

Then you wrap the function given above, by the helpfull scorpion inside this function, like so:

Code:
// Define directory
$dir = "./{$username}/";

// make directory
if (!(is_dir($dir))) {
    mkdir($dir, 0777);
  }

There is however one thing you have to have in mind, What if the usernames contain characters that are "not good"?
By "not good", I mean like:
`, æ, ø, å, etc.etc.

You either have to have a strict validation on usernames or you have to do some conversion before creating directories.
eg. You could replace spaces with _, etc.

htmlentities(), urlencode() and others might be usefull

If you want the "easy way out", you can make the directories based on the user_id, as I guess you have a unique user_id in a mysql table. This way is the best in my mind, as if one user deletes his account, another user can not "steal" his / her directory! Also it's always unique.

I also think that a CHMOD of 0755 might be better.

Olav Alexander Mjelde
Admin & Webmaster
 
does the folder that this script is in have to have a certain chmod setting?

i currently have it in a folder with 0755 settings.

and which of the three [if any] would be the correct syntax:

Code:
1
<?php
mkdir("/home/xxx/mainwebsite_html ./mkdirtesst/", 0700);
?>



2
<?php
mkdir("[URL unfurl="true"]http://"[/URL] . $_SERVER['HTTP_HOST'] ./{$username}/", 0700);
?>

3
<?php

$dir = "[URL unfurl="true"]http://"[/URL] . $_SERVER['HTTP_HOST'] ./{$username}/";

// make directory
if (!(is_dir($dir))) {
    mkdir($dir, 0777);
chdir('./');
  }
?>
 
Hi,
When concating strings, the . has to be outside of the "".
eg:
Code:
$r = "r";
$foo = "ba" . $r;

Also, dont use but use:
/path/to/dir
./dir

...Or whatever is relative to the executing script!
Just make sure that they dont have access in the doc_root!

Code:
// Define directory
$dir = "./users/{$username}/";

// make directory
if (!(is_dir($dir))) {
    if (mkdir($dir, 0755)) {
        echo "The directory <strong>{$dir}</strong> was successfully created!";
      }
    else {
        echo "Error: The directory <strong>{$dir}</strong> could not be created!";
      }
  }


Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top