Php 正在使用mkdir(),未创建目录
我正在制作一个网站,目前正在localhost中开发。我的save.php文件和上传文件夹,上面的代码保存在本地目录中Php 正在使用mkdir(),未创建目录,php,filesystems,mkdir,Php,Filesystems,Mkdir,我正在制作一个网站,目前正在localhost中开发。我的save.php文件和上传文件夹,上面的代码保存在本地目录中 $ad_title = $_POST['title']; $ad_content = $_POST['content-ads']; $ad_region = $_POST['region']; if (!is_dir("uploads/".$ad_region)) { // dir doesn't exist, make it mkdir("uploads
$ad_title = $_POST['title'];
$ad_content = $_POST['content-ads'];
$ad_region = $_POST['region'];
if (!is_dir("uploads/".$ad_region)) {
// dir doesn't exist, make it
mkdir("uploads/".$ad_region);
echo "directory created!";
}
else {
echo "directory already exist!";
}
当我重新定位目录中的save.php文件和uploads文件夹时
localhost/system/modules/new/
现在一切似乎都在运转。但我希望它能在未来发挥作用
localhost/system/
更好的组织目录。有关如何使其工作的任何帮助?您可以使用相对路径
。/
如mkdir(“../uploads/”$ad\u region)
或者使用绝对路径,例如mkdir(“/localhost/system/modules/new/”$ad\u region)
ref:您可以使用绝对文件路径,如“/var/www/system/modules/new/$ad_region”(unix结构) 或者,例如,如果您的save.php文件位于目录“system”中,并且您希望在“system/modules/new/”中创建目录,您可以这样做
localhost/system/modules/new/
mkdir还有第三个参数recursive,它允许创建嵌套目录。例如,对于第二个参数,可以简单地传递0
mkdir("./modules/new/$ad_region");
我要做的第一件事是确保路径在你认为的地方 试试这个
mkdir("./modules/new/$ad_region", 0, true);
你能描述一下它是如何工作的吗?@Phil:它不会在这个目录localhost/system/modules/new/both上的uploads文件夹中创建目录。它不会产生任何错误,但也不会创建文件夹。它只是继续打印“目录已创建!”,但在我检查uploads文件夹时没有创建目录。我已经检查了是否允许在该目录上写入。但还是不行,谢谢!现在开始工作了。你认为问题的原因是什么?@AnnaGee我唯一真正做的改变是使用
\uuuu DIR\uuu
常量作为路径前缀,确保你的上传
文件夹与PHP脚本相关。然后,使用mkdir
$ad_title = $_POST['title'];
$ad_content = $_POST['content-ads'];
$ad_region = $_POST['region'];
// Make sure the "uploads" directory is relative to this PHP file
$uploads = __DIR__ . '/uploads';
$path = $uploads . DIRECTORY_SEPARATOR . $ad_region;
// ensure that the path hasn't been tampered with by entering any relative paths
// into $_POST['region']
if (dirname($path) !== $uploads) {
throw new Exception('Upload path has been unacceptably altered');
}
if (!is_dir($path)) {
if (!mkdir($path, 0755, true)) {
// you should probably catch this exception somewhere higher up in your
// execution stack and log the details. You don't want end users
// getting information about your filesystem
throw new Exception(sprintf('Failed to create directory "%s"', $path));
}
// Similarly, you should only use this for debugging purposes
printf('Directory "%s" created', $path);
} else {
// and this too
printf('Directory "%s" already exists', $path);
}