mkdir() not working
Asked Answered
A

11

13

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.

Acceptation answered 24/6, 2010 at 18:54 Comment(10)
None, it just goes through does it's thing and doesn't spit out any errors. I even tried to see if it was somehow mistakenly placing the new directory somewhere else and I could find if anywhere.Acceptation
A couple things: Does the process running this code have permission to write into the parent directory? If you're running this through a web server, have you checked the logs for error messages?Appurtenant
so, you have to turn error reporting onMaidenly
@Col. Shrapnel: Ah, yes that's true, I had assumed it to be the case, but we know what happens when making assumptions. Thanks for pointing that out!Appurtenant
Haha yeah error reporting is on but it's not spitting any errors out, I even tried using this code #928064 And it's not coming back with any errors, the entire path is 777 from /var to www to the project directory. It's making me pull my hair out. It's gotta be something stupidly simple.Acceptation
I doubt you have your error reporting really on.Maidenly
I've mostly run out of ideas here, except to suggest that you run with command line PHP if possible (and you haven't already) to see if that will spit out an error message. If your existing script won't work without the web server, just create a short script like: <?php $retVal = mkdir("/some/absolute/path"); echo "\$retval = '" . $retVal . "'\n"; ?>Appurtenant
Besides error_reporting, is "display_errors" also set to 1 ? This is a thing i always like to forget and if this is not helping and no error is shown, i also ran out of ideas.Brinkmanship
This is the code I usually use to display errors while testing... ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); But like I said I tried that other code I found on here, so I dunno.Acceptation
Necro posting to contribute an idea - my issue was file permissions. Running the script as the /home/user, but created the directory using root via SSH.Videlicet
P
-4

You're missing quotes around the path name parameter.

Pape answered 24/6, 2010 at 18:55 Comment(2)
The code actually has that in there... Sorry in my frustration I forgot add that in, I'll make an edit. Any other ideas?Acceptation
Make sure you've got error reporting turned on so you can see if it gives any warnings when calling the function.Pape
C
15

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!

Coracle answered 24/6, 2010 at 19:18 Comment(2)
It's all good, I was afraid of that happening so I just made an edit to the original question.Acceptation
Nice to see a seven-year old answer still helps people!Coracle
E
4

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');.

Enlarge answered 25/6, 2010 at 12:45 Comment(2)
Thats pretty much the code I had at the beginning of the script. Except instead of the -1 I had this. Is it supposed to be the -1? I had ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT) Am I supposed to be using -1 instead? 1 has always worked in the past in displaying errors.Acceptation
It is a bit mask, so -1 essentially is the same as "all bits set" for unsigned int. The effect will be the same. If, in some future version, they add another E_XXX constant which is not included in E_ALL, -1 will include it anyway. Depends on what you prefer, basically.Enlarge
G
4

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);
Glary answered 24/7, 2018 at 11:56 Comment(0)
R
2

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?

Reading answered 25/6, 2010 at 12:47 Comment(0)
B
1

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.

Benevolent answered 29/10, 2012 at 8:17 Comment(0)
A
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

Abbyabbye answered 5/3, 2019 at 8:26 Comment(1)
You are an absolute angel, I needed this. For some reason none of the other answers worked, even with chmod 0755 and specifying modes in mkdirBolzano
K
1

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

Kelle answered 5/5, 2021 at 5:25 Comment(0)
L
0

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.

Lafferty answered 18/8, 2022 at 14:10 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Universalize
A
0

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

Ambler answered 18/12, 2022 at 8:36 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Naara
P
-4

You're missing quotes around the path name parameter.

Pape answered 24/6, 2010 at 18:55 Comment(2)
The code actually has that in there... Sorry in my frustration I forgot add that in, I'll make an edit. Any other ideas?Acceptation
Make sure you've got error reporting turned on so you can see if it gives any warnings when calling the function.Pape
M
-5

You must take the attribute in quotes:

mkdir('path/to/your/dir','0777');
Muire answered 24/6, 2010 at 20:59 Comment(1)
no, the $mode parameter is defined as an octal int so you should not put quotes around it.Lobachevsky

© 2022 - 2024 — McMap. All rights reserved.