php mkdir() 说没有这样的目录并且失败了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15012214/
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() says theres no such directory and fails?
提问by rpsep2
Im likely doing something very simply wrong, but when I try to make a directory (using a variable of an insert just performed as the last folder name), I get the error:
我可能做了一些非常简单的错误,但是当我尝试创建目录时(使用刚刚执行的插入变量作为最后一个文件夹名称),我收到错误:
Warning: mkdir() [function.mkdir]: No such file or directory in /home/blah/blah
警告:mkdir() [function.mkdir]:/home/blah/blah 中没有这样的文件或目录
with the code:
使用代码:
if (!is_dir("images/listing-images/rent/'.$insertID.")) {
//make new directory with unique id
mkdir("images/listing-images/rent/'.$insertID.");
}
of course the directory doesn't exist.. I'm trying to make it now? confused!
当然该目录不存在..我现在正在尝试制作它?使困惑!
采纳答案by John Conde
You have an error in your string:
您的字符串中有错误:
mkdir("images/listing-images/rent/'.$insertID.");
should be:
应该:
mkdir("images/listing-images/rent/$insertID");
回答by zerkms
It happens because you don't have images/listing-images/rentpath existing in your filesystem.
发生这种情况是因为您images/listing-images/rent的文件系统中没有路径。
If you want to create the whole path - just pass the 3rd argument as a true:
如果要创建整个路径 - 只需将第三个参数作为 a 传递true:
mkdir('images/listing-images/rent/'.$insertID, 0777, true);
There is also a chance you're in a wrong directory currently. If this is the case - you need to change the current dir with chdir()or specify the full path.
您当前也有可能位于错误的目录中。如果是这种情况 - 您需要使用chdir()或指定完整路径来更改当前目录。
回答by adamdunson
Assuming you're using PHP > 5.0.0, try mkdir("path", 0777, true);to enable creating directories recursively (see here: http://php.net/manual/en/function.mkdir.php).
假设您使用的是 PHP > 5.0.0,请尝试mkdir("path", 0777, true);启用递归创建目录(请参阅此处:http: //php.net/manual/en/function.mkdir.php)。
回答by Ramyz
- recursive Allows the creation of nested directories specified in the path name.
- but did not work for me!! for that here is what i came up with!!
- and it work very perfect!!
- recursive 允许创建在路径名中指定的嵌套目录。
- 但对我不起作用!!因为这就是我想出的!!
- 它工作得非常完美!!
$upPath = "../uploads/RS/2014/BOI/002"; // full path
$tags = explode('/' ,$upPath); // explode the full path
$mkDir = "";foreach($tags as $folder) { $mkDir = $mkDir . $folder ."/"; // make one directory join one other for the nest directory to make echo '"'.$mkDir.'"<br/>'; // this will show the directory created each time if(!is_dir($mkDir)) { // check if directory exist or not mkdir($mkDir, 0777); // if not exist then make the directory } }
$upPath = "../uploads/RS/2014/BOI/002"; // 完整路径
$tags = expand('/' ,$upPath); //
分解完整路径$mkDir = "";foreach($tags as $folder) { $mkDir = $mkDir . $folder ."/"; // make one directory join one other for the nest directory to make echo '"'.$mkDir.'"<br/>'; // this will show the directory created each time if(!is_dir($mkDir)) { // check if directory exist or not mkdir($mkDir, 0777); // if not exist then make the directory } }
回答by user3410311
in my case $insertID was generated from some data as string by concatinating
在我的情况下 $insertID 是通过连接从某些数据作为字符串生成的
$insertID=$year.$otherId;
I simple rewrote code like this and error disappeared:
我简单地重写了这样的代码,错误消失了:
$insertID=(int)($year.$otherId);
回答by Luca Antonelli
Probably the real error was that he forgot an extra apex.
可能真正的错误是他忘记了一个额外的顶点。
This:
这个:
mkdir("images/listing-images/rent/'.$insertID.");
Inside:
里面:
/'.$insertID."
Correct Version:
正确版本:
/".$insertID
Extended Correct Version:
扩展正确版本:
mkdir("images/listing-images/rent/".$insertID);
回答by CaffeinatedDave
You shouldn't use is_dir() to check if something exists, you want file_exists() as well. Try:
你不应该使用 is_dir() 来检查是否存在某些东西,你也需要 file_exists() 。尝试:
if (file_exists("images/listing-images/rent/$insertID") {
mkdir("images/listing-images/rent/$insertID.");
}
Have taken the '. out since it looks like a syntax error, but you might have a legitimate reason to keep it in.
采取了'。出,因为它看起来像一个语法错误,但你可能有合法的理由保留它。
If the mkdir still fails, it could be that images/listing-images/rent doesn't exist, you'll have to create that separately if so.
如果 mkdir 仍然失败,则可能是 images/listing-images/rent 不存在,如果是这样,您必须单独创建。

