Create a new directory having 0777 permissions

I need to create unique directory for every new member, which will be used to store their pictures. I’ve tried :

mkdir("user_name", 0777);

I got : mkdir() [function.mkdir]: Permission denied

It work when I chmod parent directory but can not give permissions “0777”, therefore I add umask()

umask(000);
mkdir("user_name", 0777);

Eurêka BUT ! the new folder is now owned by mr apache

Owned by apache

Now imagine that we want to create a subfolder like “user_name/gallery” ! we will get an error like :

Warning: mkdir() [function.mkdir]: SAFE MODE Restriction in effect. The script whose uid is XXX is not allowed to access /var/www/vhosts/updel.com/httpdocs/test/test owned by uid 48 in /var/www/vhosts/updel.com/httpdocs/test.php on line 4

It mean we do not have permission to touch a directory owned by another user !!!!!!!!

The solution : FORGET mkdir() and through ftp using PHP, the code look like this

$connexion_ftp = ftp_connect("Ftp_url"); //Connexion ftp
ftp_login($connexion_ftp, "Ftp_login", "Ftp_Password");
ftp_mkdir($connexion_ftp, "new_dir"); // user_name in my case
ftp_site($connexion_ftp,"CHMOD 777 new_dir");