View Full Version : mkdir() not working

04-11-2012, 11:50 AM
Hey all,

I'm currently building a website where users can create pages which pictures can eventually be uploaded to. I want it so when a new page is created a folder is made on the server specific to that event which will then contain all photo's for that event.

This is what I'm currently working with but for some reason just isn't working:

Attempt style 1:

$newfolder = mkdir("../imagehub/events/".$evt.$name, 0777);

Attempt style 2:

$newfolder = mkdir("../imagehub/events/$evt$name", 0777);

Attempt style 3:

echo "File not created";
echo "File Created";

Attempt style 4:

$newfolder = "../imagehub/events/".$evt.$name;
mkdir($newfolder, 0777);

I presume I'm going wrong somewhere but a little nudge in the right direction would be much appreciated! :)

04-11-2012, 03:40 PM
It may be a server permissions problem - quite common these days.

04-11-2012, 04:17 PM
Make sure to check your error logs as well. I can see at least two common ones; the first as mentioned is a simple permission issue, which may be resolvable via a chmod or umask. Typically you'll need to set the ownership of the ../imagehub/events directory to that of your apache user (nobody, www-user, apache, different names I'm afraid), group it to apache, or allow write access to everyone. I'd go for either own or group with read, write, execute. The easiest fix is to let PHP make the directories for you, they should automatically take the ownership to the apache user.

The other issue that could be common is a simple incorrect filepath. This is relative, it assumes that you will find this path a directory above the executing script. If it can't find the ../imagehub/events path, it will fail. The default handing of mkdir is to not recursively create the directory structure for you (can be forced with the third parameter, but I won't recommend doing so until you at least determine what the issue is).

04-11-2012, 04:36 PM
Thanks both of you. I managed to get it to work by changing the path name to another area. Not sure what was the issue with the original one but it seems to be fine now. Thanks again.