Springheeled Jack
Springheeled Jack

Reputation: 199

Creating a directory and inserting another director inside that directory in PHP

I have a directory on my root called profiles.

I want to add another dir into that and another inside that something like this

profiles > jack > user_images

$username_entry = "jack";
$user_image_dir = "user_images";

mkdir(profiles."/".$username_entry, 0777, true);
mkdir(profiles."/".$username_entry."/".$user_image_dir, 0777, true);

But i dont think this is the correct way to do it.

Can anyone help me do this the correct way.

Upvotes: 1

Views: 41

Answers (2)

Luthando Ntsekwa
Luthando Ntsekwa

Reputation: 4218

That is correct, you can also check if the directory does not exist before creating it:

if (!file_exists("profiles/".$username_entry)) {
       mkdir("profiles/".$username_entry, 0777, true);

}

EDIT:
To create the directory in the root, we can use $_SERVER['DOCUMENT_ROOT']:

  if (!file_exists($_SERVER['DOCUMENT_ROOT']."/profiles/".$username_entry))  
    {
       mkdir($_SERVER['DOCUMENT_ROOT']."/profiles/".$username_entry, 0777, true);
    }

Upvotes: 1

Reversal
Reversal

Reputation: 622

If you are executing your script from root directory this one line should work as intended:

mkdir("/profiles/".$username_entry."/".$user_image_dir, 0777, true);

Upvotes: 0

Related Questions