简体   繁体   中英

Warning: mkdir(): Permission denied

I am trying to make a directory when a new account is created.
The directory should be in my images folder and is used to better separate uploaded images.

//get the ID of the new account that was just created/inserted
$accountID = mysqli_insert_id($dbc);

//create a new directory path for that account
$directoryPath =  "../images/" . $accountID;

// check if the directory exists
if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath, 0777);         //breaking here
}      

I had no problem getting this to work a few days ago, however when testing it today I am having problems.

I have included ini_set('display_errors', 'On'); in my page to see what error I am being thrown and it is a permission error.

Warning: mkdir(): Permission denied

The images folder has full read/write permissions for all users and groups as well as any parent folders so I don't understand how that would be an issue, that and it had worked several times before.

I am working on windows if that matters.

Any ideas?

To avoid spending too much time on permissions problems between the CLI user and the Apache user, an easy configuration is to use same user for both processes.

Get your user id and group by doing

$ id
uid=1000(my_user), gid=1000(my_group), ...

And then:

$ sudo service apache2 stop
$ sudo vi /etc/apache2/envvars
export APACHE_RUN_USER=my_user
export APACHE_RUN_GROUP=my_group
$ sudo chown -R my_user /var/lock/apache2

it's better and safer than to change you whole directory permission to 777

You have to be sure that the parent directory allows you to create folder, and not the folder it self that is being created with 0777 rights...
Also, check with which user the Apache server is launched
Check if directory is already exist before using mkdir()

if (!is_dir ($directoryPath) ) {
    mkdir($directoryPath, 0777);
}

I think you should try this -

mkdir("../images/".$accountID, 0777, 'R');

Recursive folder creation may causing the problem. Also get more information from - mkdir

Also check for folder permission.

I am working on windows if that matters.

It does.

Try changing this

if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath, 0777);         //breaking here
}   

To this

if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath);         //breaking here
}   

You are on a Windows box so it will ignore the chmod mode. Also try using the full paths and not relative.

The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod() page.

Note:mode is ignored on Windows .

http://php.net/manual/en/function.mkdir.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM