Mike Keller
Mike Keller

Reputation: 615

mkdir() not working

My code

mkdir("/some/absolute/path",0777);

and

mkdir("relative/path", 0777);

is not working, safe mode is turned off, and I've even tried setting all of the parent folders to 777.

Any ideas?

EDIT: I do have error reporting turned on, in my frustration I've 777'd the whole path just to make sure that, that isn't the issue. It's gotta be something stupidly simple going on.

EDIT EDIT: Upvotes for everyone who responded with suggestions... But I'm not going to select an answer since this still isn't resolved, but then again I think this is going to be one of those ones left open forever.

EDIT x 3: So I have the most unsatisfactory resolution to this question ever... I started with a clean VM image, retried it and it works now. No joke.

Upvotes: 13

Views: 52510

Answers (11)

mohamad zekri
mohamad zekri

Reputation: 1

chown www-data:www-data (address directory)

Upvotes: 0

Deepak Dubey
Deepak Dubey

Reputation: 1

mkdir only creates one single directory when called without -p.

A directory in the path /usr/local/myfolder/ is missing, this is why you get the error. If you call mkdir -p, the missing path is created as well.

Another effect of using the -p option is that mkdir -p does not complain when the directory already exists. This is why this variant is frequently used in scripts.

Upvotes: 0

brandon
brandon

Reputation: 21

Make sure the parent directories have correct write permissions, that was my problem

Upvotes: 1

For future references, the problem might come from the possibility that the directory where you're trying to create your new directory doesn't have enough permission.

For instance, your index directory might look like this: index.php new_dirs_here

if new_dirs_here doesn't have enough permission then you can't create direcories inside.

To solve this, I'd use the command: chmod 777 new_dirs_here

I'm not worrying about security now, Just trying to solve the immediate problem. You could of course look up a better permission settings, but the idea is that your new_dirs_here should have enough permissions.

Then, your mkdir() dunction should work just fine.

Good luck

Upvotes: 1

Declan Land
Declan Land

Reputation: 650

If anyone gets stuck with this problem.. there's one answer I can give you that I spend 2 hours on finding.. I tried with using a full path, and "../mydirectoryname".

Try using:

mkdir("./mydirectoryname", 0777, true);

Instead of..

mkdir("../mydirectoryname", 0777, true);

Upvotes: 4

I have similar problem and I found out, that I have no free space left on my drive. Check with command df (on linux) how full is your drive. It is possible that root is allowed to create files and folders in this situation, because he has pre-reserved space. If you run you script from command-line as root user - there is no error, but if your script is run by apache, then error occure.

Upvotes: 1

Dominique
Dominique

Reputation: 908

Have you tried with the shortest test possible?

mkdir('directory',0777);

If this does not work I would try creating with a standard CHMOD like 0755 (this is a totally random guess, maybe the server won't allow creating 0777 via PHP)

if this don't work I would say the server probably need different setup / php doesn' thave the writting right on folder, maybe you could ask your host provider?

Upvotes: 2

janmoesen
janmoesen

Reputation: 8020

Are you trying to create those directories recursively, like you would do with mkdir -p on the command line? If so, specify true as the third parameter to mkdir.

And just to echo the previous suggestions, PLEASE specify the error messages you are getting. If you are not getting any, use this before your call: error_reporting(-1); // ALL messages and ini_set('display_errors', 'On');.

Upvotes: 4

Alex Pliutau
Alex Pliutau

Reputation: 21957

You must take the attribute in quotes:

mkdir('path/to/your/dir','0777');

Upvotes: -5

ajm
ajm

Reputation: 20105

Do all of the parent directories exist?

If not, you'll need to enable recursion (assuming PHP5 here):

mkdir('/path/to/your/dir',0777,true);

EDIT: Didn't see the hidden comment saying that every directory from var downward was set to world-writable, so I'm betting the directory path exists and the above won't be helpful. Sorry!

Upvotes: 15

Daniel Egeberg
Daniel Egeberg

Reputation: 8382

You're missing quotes around the path name parameter.

Upvotes: -4

Related Questions