Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To Create Multiple Directories using mkdir() in Php

Tags:

php

cpanel

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)
like image 402
Faizan Yousuf Bhakhrani Avatar asked Jun 01 '26 03:06

Faizan Yousuf Bhakhrani


1 Answers

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);
like image 102
Barmar Avatar answered Jun 03 '26 16:06

Barmar