Create A Directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Create A Directory

Post by icesolid »

Is it possible to have PHP create a directory, and if so is it possible to set the permissions of this directory to read and write?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

chmod('0777', 'somedir');
mkdir('somedir');
User avatar
sman317
Forum Newbie
Posts: 10
Joined: Wed Jun 01, 2005 11:47 am

Post by sman317 »

Two steps:
1. mkdir("/path/to/my/dir"); //Makes the directory
2. chmod("/path/to/my/dir", 0644); //Changes the permissions to Read and write for owner, read for everybody else
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Errors

Post by icesolid »

I get these two errors when using the code above:

Warning: mkdir(somedir): Permission denied in /home/capous/public_html/test.php on line 2

Warning: chmod(): No such file or directory in /home/capous/public_html/test.php on line 3


Also I want this new folder to be created in a existing directory named 'photos', this directory is located in my www main directory.
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

umm... I believe it's part of the mkdir function

mkdir( "dir", 0777);
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

php.net, the first resource before phpdevnetwork... http://us3.php.net/mkdir
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I figured it out.

The chmod code that was posted was in the wrong order, the directory location goes first, then permissions.

And yes, I did use the manual, thank you!
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

The only thing that seems weird is that if I use the code below it sets the directory at 357, which doesent give group permission to view the directory.

Code: Select all

mkdir("/home/capous/public_html/photos/" . $control_number . "", "0700");
chmod("/home/capous/public_html/photos/" . $control_number . "", "0750");
I want the directory permission to be 757, however if I use 0757 is chmod it doesent work.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Try 0755. Sorry about the reversal! :oops:
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

No its not working.

Here is my code:

Code: Select all

include("connect.php");

$result = mysql_query("SELECT * FROM cases ORDER BY control_number DESC");
$row = mysql_fetch_array($result);
$control_number = $row["control_number"] + "1";

mkdir("/home/capous/public_html/photos/" . $control_number . "", "0700");
chmod("/home/capous/public_html/photos/" . $control_number . "", "0755");
It creates the directory, but it sets it two 363 now?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may be hitting a umask restriction that disallows creations of that permission.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Someone tell me if I'm kracked, but if you are making a directory in a directory that isn't 0777 you won't get anywhere.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

The photos dir is set at 777.

I want the directories created in photos to be 757 or 777, neither is working, and I don't even see 757 or 777 on php.net.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Like feyd said, mkdir() may be a banned function on your server. check with your host.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I never said mkdir() may be banned. I was talking about umask().
Post Reply