mkdir permission denied php
PHP mkdir Permission Denied running on Windows Server 2008 IIS 7 due to Read Only Attribute?
I’m having a problem with a PHP website running on IIS 7 on Windows Server 2008.
There is one line of code calling mkdir which is erroring and the error log reads:
I have ruled out anything to do with folder permissions (I have tried multiple groups: Everyone, Users, IUSR, Network Service etc. with no luck).
I need to know how mkdir works, does it check the read-only attribute of the parent folder?
Bounty awarded to @BOMEz because of the useful quote from mkdir() documentation which indicated that I should double-think the permissions. @BOMEz also provided a tailored answer and interacted with me via comments which helped.
4 Answers 4
As a test (preferably in a development environment) give the IIS user full access to the parent folder. If this makes it work, slowly start taking away privileges to see which ones you need.
Windows ignores the mode option. Might be some weird bug causing it to fail.
I’ve ran into situations with windows before where access was denied attempting to use a relative path, but the full path works just fine.
EDIT: Looking at the comments on the documentation for mkdir() one commenter mentions that you might also need to add execute permissions to the user:
If you’re getting a Permission Denied error, but are certain the permissions and ownership where you are trying to create the directory are correct, check again:
The location where you are trying to create the directory in must have the Execute permission for the owner trying to create it, regardless of if the folder is Readable, or Writable.
This may be obvious to some, but was not to me at first. Hopefully this will save you the trouble I went through.
Since you didn’t mention a control panel of any sort I’m going to assume you have access to the server either physical or remote desktop. I’m also going to assume you checked your php.ini settings.
That being said, there is a work around for some of these permission problems.
You will need to create an administrator account (and add it to the administrators group) for this site to use or simply use your administrator credentials.
This is a bit of a sledge hammer fix since it will grant full unrestricted access to the file system from your scripts.
If you created a new user and you want to maintain some level of script security then you can try pulling the user out of the administrators group and then giving it full permissions on only that site with the following:
If asked, select that you want to apply to all directories and files.
If your still having issues then there is a good chance your php setup is configured incorrectly or corrupt. (Safe mode turned on maybe?)
PHP mkdir: Permission denied problem
13 Answers 13
I know this is an old thread, but it needs a better answer. You shouldn’t need to set the permissions to 777, that is a security problem as it gives read and write access to the world. It may be that your apache user does not have read/write permissions on the directory.
Here’s what you do in Ubuntu
Make sure all files are owned by the Apache group and user. In Ubuntu it is the www-data group and user
Next enabled all members of the www-data group to read and write files
The php mkdir() function should now work without returning errors
Late answer for people who find this via google in the future. I ran into the same problem.
NOTE: I AM ON MAC OSX LION
What happens is that apache is being run as the user «_www» and doesn’t have permissions to edit any files. You’ll notice NO filesystem functions work via php.
Open a finder window and from the menu bar, choose Go > Go To Folder > /private/etc/apache2
now open httpd.conf
change the username:
Now restart apache by running this form terminal:
If it still doesn’t work, I happen to do the following before I did the above. Could be related.
Open terminal and run the following commands: (note, my webserver files are located at /Library/WebServer/www. Change according to your website location)
Don’t set permissions to 777 when using mkdir in PHP
Link only answers are not considered good practice on StackOverflow, but the advice that is given here should generally NOT be followed up.
I would like to revert to this great answer on a similar question. I quote:
Please stop suggesting to use 777. You’re making your file writeable by everyone, which pretty much means you lose all security that the permission system was designed for. If you suggest this, think about the consequences it may have on a poorly configured webserver: it would become incredibly easy to «hack» the website, by overwriting the files. So, don’t.
How to fix Error: mkdir(): Permission denied when running composer
I have a ec2 image and get the following error when trying to create a new laravel project.
[ErrorException] mkdir(): Permission denied
Here is the command:
I can write to the folder with my ec2-user username but do I need to add permission for composer to write?
10 Answers 10
For security reasons, lets keep root as the owner and just allow ourselves to read, write, and execute within the area which we will be working in.
Change group ownership
Add priviledges to our group
For the grand finale, go ahead and create your new laravel project
Congratulations, We’re done. 🙂
I still want root to be the owner of the files for security reason.
I am working in a localhost so there shouldn’t be any other (guest)users writing on my files.
In order to achieve this we will need to edit the group ownership and add the proper permissions. We will leave the others as is and only allow them to read and execute the files.
So lets get started:
I like being in the directory that I am making changes to in order to avoid mistakes so first we will need to enter the the directory which we will be working in.
We will need to change the group ownership of the directories which we will be working in. Starting from the directory which we are in and all the future directories and files we will create underneath this directory. So basically any children directories from now on will be own by our group.
. = This just means ‘here, in this directory’.
chmod = command to change modifications, in this case, privileges.
-Rv = Recursive & Verbose
g = this states who will receive the modifications. In our case g-group Other options are u-user and o-other.
r = symbolizes read
w = symbolizes write
If you get this: drwxrw-r-x
You are missing the executable( x ) privilege for the group. The first x is for the user and last x is for others. There should be middle x for the group.
Run the following command and you should be all set to go:
mkdir
(PHP 4, PHP 5, PHP 7, PHP 8)
mkdir — Создаёт директорию
Описание
Список параметров
Аргумент permissions игнорируется в Windows.
Возвращаемые значения
Возвращает true в случае успешного выполнения или false в случае возникновения ошибки.
Ошибки
Примеры
Пример #1 Пример использования функции mkdir()
Пример #2 Использование mkdir() с параметром recursive
// Желаемая структура папок
$structure = ‘./depth1/depth2/depth3/’ ;
Смотрите также
User Contributed Notes 40 notes
When using the recursive parameter bear in mind that if you’re using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. ie:
May result in «/test1/test2» having a mode of 0777 but «/test1» still having a mode of 0755 from the mkdir() call. You’d need to do:
Please note that in a shared environment I failed to take into account an existing umask when I did a mkdir(dirname, 0755). This ended up creating the directory (function returned true), but I didn’t have rights to do anything inside the folder, nor could I even view that it existed via ftp.
However, file_exists(dirname) returned true. Eventually I figured out what happened and was able to rmdir(dirname), then created the directory correctly.
So, when writing scripts you expect to be portable, either use umask to set your umask accordingly, or do a straight mkdir(dirname) followed by chmod(dirname, 0755) (or whatever it is you’re looking for). If you make the same mistake I did, you should be able to rmdir() or chmod() the folder so it’s accessible.
One small correction on a note from Frank in June 2006 on recursive directories under Windows.
Franks note stated:
This will work a bit better 🙂
Before you yell DUPLICATE! and banish me into the abyss, hear me out. 🙂
I’ve been developing an intranet site for the company I work with, and originally I was developing it directly from my workstation running Windows 7 / IIS. As I’ve neared completion of the site/applications, I was given a Centos 7 box that’s running Apache, so I’ve begun the process of migrating it over to that system. I’ve sorted out most of the minor issues, and got the site running now. However, there is a portion of the site that interacts with our database and creates a log directory/file based upon the person that’s logged in. This portion utilizes the mkdir() function, but I’m getting permission issues with it.
Here is what I’ve already done:
So whether I use chmod or chown to change the permissions for the folders being accessed, I’m getting the same type of error. To be on the safe side, I checked the PHP user also, and it’s using the apache user as well.
If anyone may have additional insight as to why it isn’t working, even with the permissions changed, then please enlighten me. If I happened to miss the one article that explained this particular situation, then yell duplicate and banish me to the abyss. lol 🙂
EDIT Okay, so upon further testing, I’ve discovered 2 issues that seem to be causing this overall issue.
Just to note, I’ve attempted to set the recursive value to true on mkdir in PHP, but that’s when I get the permission issues originally noted. When recursive/mode is removed, it doesn’t get the permission issue, but it isn’t able to create nested directories.
EDIT2
To test my theories, I removed the nesting and tried to make mkdir create just 1 directory, and it’s generating the same errors as before. Although it’s owned by Apache, and even if I set it to 777, it throws back permission issues.