php mkdir() chmod 和权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3764973/
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
php mkdir() chmod and permissions
提问by Thomas
i was using this basic script:
我正在使用这个基本脚本:
$folderPath = "../path/to/$folder/";
mkdir("$folderPath");
i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser
我创建这个目录,然后上传照片到它。我已经这样做了 4-5 个月了,当我尝试通过网络浏览器查看文件夹的内容时,突然间我开始收到“禁止”错误
The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos
目录创建相同,照片仍在上传没有问题,但我无法访问照片
I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all
我尝试重写脚本并使用 chmod 来更改权限,但我一点运气都没有
All the older folders were being created with: -w- rwx r-x r-x
使用以下命令创建所有较旧的文件夹:-w- rwx rx rx
and I can't get this recreated
我无法重新创建它
I've tried adding a chmod line into my script:
我试过在我的脚本中添加一个 chmod 行:
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
but I can't recreate the same permissions, I'm trying to understand how chmod works, but I can't figure out how to get this very basic function working properly again
但我无法重新创建相同的权限,我试图了解 chmod 的工作原理,但我无法弄清楚如何让这个非常基本的功能再次正常工作
回答by Knowledge Craving
Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-
尝试查找 HTAccess 文件,其中将提到“Options -Indexes”选项,因为这主要用于不在 Web 浏览器中显示文件夹的内容。该文件需要按以下方式搜索:-
- In the folder "
root_folder/sales/inventory/$folder/
", where "$folder
" is as mentioned in your code. - If not found, try in the folder "root_folder/sales/inventory/".
- If not found, try in the folder "root_folder/sales/".
- If not found, try in the folder "root_folder/".
- 在文件夹“
root_folder/sales/inventory/$folder/
”中,其中“$folder
”在您的代码中提到。 - 如果未找到,请在文件夹“root_folder/sales/inventory/”中尝试。
- 如果未找到,请在文件夹“root_folder/sales/”中尝试。
- 如果未找到,请在文件夹“root_folder/”中尝试。
When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder
", where the code will be "Options Indexes".
当你得到写在 HTAccess 文件中的“Options -Indexes”的代码时,你可以从那里删除/注释那行代码,或者你也可以在你需要的“ $folder
”文件夹中编写另一个 HTAccess 文件,代码将在那里“选项索引”。
Also in the PHP page, the logic must be like this:-
同样在PHP页面中,逻辑必须是这样的:-
<?php
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
// Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable.
chmod($targetFile, 0755);
?>
This will help you when you will be unlinking / deleting the uploaded files from the "$folder" folder.
当您从“$folder”文件夹中取消链接/删除上传的文件时,这将对您有所帮助。
Hope it helps.
希望能帮助到你。
回答by quayph
If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.
如果您的 $folder 变量包含一些子目录,您的父目录可能没有被修改为正确的权限。这是我在租用的 OVH Gentoo 服务器上遇到的问题。
Imagine that $folder = '/store1/ally23/shelf42';
so your final directory structure is
../sales/inventory/store1/ally23/shelf42
, and you want 0777
permisions.
You do:
想象一下,$folder = '/store1/ally23/shelf42';
您的最终目录结构是
../sales/inventory/store1/ally23/shelf42
,并且您需要0777
权限。你做:
mkdir($folderPath, 0777, true) || chmod($folderPath, 0777);
Only the final directory shelf42
is chmoded to 0777
. The intermediary directories are created with default permissions (in my case 0744
).
只有最终目录shelf42
被 chmoded 为0777
. 中间目录是使用默认权限创建的(在我的情况下0744
)。
There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.
PHP 的 chmod 命令中没有递归选项,因此您必须遍历中间目录并单独对它们进行 chmod。
回答by Mark Mutti
If you're in a shared environment, you may also want to chownafter upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.
如果您在共享环境中,为了安全起见,您可能还想在上传后chown。特别是如果您在虚拟主机以外的用户下运行您的 Web 服务器有权访问(例如:“nobody”与“mysite”。)这在 cPanel 服务器中很常见,FWIW。