php mkdir() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3113028/
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
mkdir() not working
提问by Mike Keller
My code
我的代码
mkdir("/some/absolute/path",0777);
and
和
mkdir("relative/path", 0777);
is not working, safe mode is turned off, and I've even tried setting all of the parent folders to 777.
不起作用,安全模式已关闭,我什至尝试将所有父文件夹设置为 777。
Any ideas?
有任何想法吗?
EDIT: I do have error reporting turned on, in my frustration I've 777'd the whole path just to make sure that, that isn't the issue. It's gotta be something stupidly simple going on.
编辑:我确实打开了错误报告,在我的沮丧中,我已经 777 完成了整个路径,只是为了确保这不是问题。一定是发生了一些非常简单的事情。
EDIT EDIT: Upvotes for everyone who responded with suggestions... But I'm not going to select an answer since this still isn't resolved, but then again I think this is going to be one of those ones left open forever.
编辑 编辑:为所有回答建议的人点赞...但我不会选择答案,因为这仍未解决,但我认为这将是永远开放的答案之一。
EDIT x 3: So I have the most unsatisfactory resolution to this question ever... I started with a clean VM image, retried it and it works now. No joke.
编辑 x 3:所以我对这个问题有最不满意的解决方案......我从一个干净的 VM 映像开始,重试它,现在它可以工作了。不是开玩笑。
采纳答案by Daniel Egeberg
You're missing quotes around the path name parameter.
您缺少路径名参数周围的引号。
回答by ajm
Do all of the parent directories exist?
所有的父目录都存在吗?
If not, you'll need to enable recursion (assuming PHP5 here):
如果没有,您需要启用递归(假设这里是 PHP5):
mkdir('/path/to/your/dir',0777,true);
EDIT: Didn't see the hidden comment saying that every directory from vardownward was set to world-writable, so I'm betting the directory path exists and the above won't be helpful. Sorry!
编辑:没有看到隐藏评论说每个目录从var下往上都被设置为世界可写,所以我打赌目录路径存在并且以上不会有帮助。对不起!
回答by janmoesen
Are you trying to create those directories recursively, like you would do with mkdir -pon the command line? If so, specify trueas the third parameter to mkdir.
您是否尝试递归创建这些目录,就像您mkdir -p在命令行上所做的那样?如果是,请指定true为的第三个参数mkdir。
And just to echo the previous suggestions, PLEASE specify the error messages you are getting. If you are not getting any, use this before your call: error_reporting(-1); // ALL messagesand ini_set('display_errors', 'On');.
为了回应之前的建议,请指定您收到的错误消息。如果你没有得到任何,在你打电话之前使用它:error_reporting(-1); // ALL messages和ini_set('display_errors', 'On');。
回答by Dominique
Have you tried with the shortest test possible?
您是否尝试过最短的测试?
mkdir('directory',0777);
mkdir('目录',0777);
If this does not work I would try creating with a standard CHMOD like 0755 (this is a totally random guess, maybe the server won't allow creating 0777 via PHP)
如果这不起作用,我会尝试使用像 0755 这样的标准 CHMOD 创建(这是一个完全随机的猜测,也许服务器不允许通过 PHP 创建 0777)
if this don't work I would say the server probably need different setup / php doesn' thave the writting right on folder, maybe you could ask your host provider?
如果这不起作用,我会说服务器可能需要不同的设置/ php 没有正确的文件夹,也许您可以询问您的主机提供商?
回答by Adam Sádovsky
I have similar problem and I found out, that I have no free spaceleft on my drive. Check with command df(on linux) how full is your drive. It is possible that root is allowed to create files and folders in this situation, because he has pre-reserved space. If you run you script from command-line as rootuser - there is no error, but if your script is run by apache, then error occure.
我有类似的问题,我发现我的驱动器上没有可用空间。使用命令df(在 linux 上)检查您的驱动器有多满。在这种情况下,root 可能被允许创建文件和文件夹,因为他有预先保留的空间。如果您以root用户身份从命令行运行脚本- 没有错误,但如果您的脚本由 运行apache,则会发生错误。
回答by Declan Land
If anyone gets stuck with this problem.. there's one answer I can give you that I spend 2 hours on finding.. I tried with using a full path, and "../mydirectoryname".
如果有人遇到这个问题.. 我可以给你一个答案,我花了 2 个小时来查找.. 我尝试使用完整路径和“../mydirectoryname”。
Try using:
尝试使用:
mkdir("./mydirectoryname", 0777, true);
Instead of..
代替..
mkdir("../mydirectoryname", 0777, true);
回答by Chihab
For future references, the problem might come from the possibility that the directory where you're trying to create your new directory doesn't have enough permission.
对于将来的参考,问题可能来自您尝试创建新目录的目录没有足够的权限。
For instance, your index directory might look like this:
index.php new_dirs_here
例如,您的索引目录可能如下所示:
index.php new_dirs_here
if new_dirs_heredoesn't have enough permission then you can't create direcories inside.
如果new_dirs_here没有足够的权限,那么你不能在里面创建目录。
To solve this, I'd use the command:
chmod 777 new_dirs_here
为了解决这个问题,我会使用以下命令:
chmod 777 new_dirs_here
I'm not worrying about security now, Just trying to solve the immediate problem. You could of course look up a better permission settings, but the idea is that your new_dirs_hereshould have enough permissions.
我现在不担心安全性,只是想解决眼前的问题。您当然可以查找更好的权限设置,但我们的想法是您new_dirs_here应该拥有足够的权限。
Then, your mkdir()dunction should work just fine.
然后,您的mkdir()dunction 应该可以正常工作。
Good luck
祝你好运
回答by Alex Pliutau
You must take the attribute in quotes:
您必须将属性放在引号中:
mkdir('path/to/your/dir','0777');

