To Create Multiple Directories using mkdir() in Php

I am working on a social network website, in which multiple directories are created on signup. Here is the code I am using:

$path = "fb_users/Organization/" . $user . "/Profile/";
$path2 = "fb_users/Organization/" . $user . "/Post/";
$path3 = "fb_users/Organization/" . $user . "/Cover/";
mkdir($path, 0, true);
mkdir($path2, 0, true);
mkdir($path3, 0, true);

The code is working good on my localhost, but when I am using the same code on cPanel hosting, the code only creates fb_users/Organization/[email protected] (let $user = [email protected]). It's not creating three more folders. Will anyone please get me out of this?

Directory before code:

/fb_users/Organization

After running code on localhost:

/fb_users/Organization/[email protected]/Cover
/fb_users/Organization/[email protected]/Post
/fb_users/Organization/[email protected]/Profile

Same code running on hosting using cPanel:

/fb_users/Organization/[email protected] (only this directory is created)

Upvotes: 3

Views: 1316

Answers (2)

Jantho1990
Jantho1990

Reputation: 85

Try changing the second parameter to 0700.

Edit: Barmar beat me to it, with a better explanation.

Upvotes: 1

Barmar
Barmar

Reputation: 781004

When you specify mode = 0 on a Unix server, it will create the top-level directory /fb_users/Organization/[email protected] with no read or write permissions, even for the owner. So it won't have permission to create the subdirectories. Use 0700 to give the owner full permissions, but not allow anyone else to access it.

mkdir($path, 0700, true);
mkdir($path2, 0700, true);
mkdir($path3, 0700, true);

Upvotes: 5

Related Questions