php 警告:mkdir():权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29413786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mkdir(): Permission denied
提问by BRBT
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.
我已经ini_set('display_errors', 'On');
在我的页面中包含了我被抛出的错误,这是一个权限错误。
Warning: mkdir(): Permission denied
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.
如果这很重要,我正在 Windows 上工作。
Any ideas?
有任何想法吗?
回答by Freelancer
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.
为了避免在 CLI 用户和 Apache 用户之间的权限问题上花费太多时间,一个简单的配置是对两个进程使用相同的用户。
Get your user id and group by doing
通过执行获取您的用户 ID 和组
$ 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
这比将整个目录权限更改为 777 更好更安全
回答by Sandip Karanjekar
回答by Halayem Anis
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 0777rights...
Also, check with which user the Apacheserver is launched
Check if directory is already exist before using mkdir()
您必须确保父目录允许您创建文件夹,而不是使用0777权限创建的文件夹本身...
此外,请检查启动Apache服务器的用户
在使用之前检查目录是否已存在mkdir()
if (!is_dir ($directoryPath) ) {
mkdir($directoryPath, 0777);
}
回答by Panama Hyman
I am working on windows if that matters.
如果这很重要,我正在 Windows 上工作。
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 Windowsbox so it will ignore the chmod mode. Also try using the full paths and not relative.
您在Windows 机器上,因此它将忽略 chmod 模式。还可以尝试使用完整路径而不是相对路径。
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.
模式默认为 0777,这意味着可能的访问范围最广。有关模式的更多信息,请阅读 chmod() 页面上的详细信息。
注意:模式在 Windows 上被忽略。