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");

Comments (3)

Mohammed CHERIFISeptember 8th, 2009 at 10:32 am

This generally happens when safe mode or openbase_dir directives are set to ON in the php configuration file php.ini!

using ftp_connect is a nice alternative! but the only weakness is that we are forced to store the ftp credential data on the web hosting!

I think that editing the parent folder permissions via ftp can resolve the problem

PolpravOctober 17th, 2009 at 2:08 am

Hello from Russia!
Can I quote a post in your blog with the link to you?

UpDeLOctober 17th, 2009 at 11:36 am

Yes you can ;)

Leave a comment

Your comment