$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!";
}
我正在创建一个网站,我现在正在localhost中开发它。我的 save.php 文件和上传文件夹,其中上述代码保存在本地目录中
localhost/system/modules/new/
当我在目录
中重新定位 save.php 文件和上传文件夹时localhost/system/
现在一切似乎都在起作用。但是我想让它在中工作
localhost/system/modules/new/
更好组织的目录。有关如何使其工作的任何帮助?
答案 0 :(得分:1)
我要做的第一件事就是确保路径是您认为的路径。
试试这个
$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);
}
答案 1 :(得分:0)
您可以使用相对路径../
,例如mkdir("../uploads/".$ad_region)
或使用绝对路径,例如mkdir("/localhost/system/modules/new/".$ad_region)
答案 2 :(得分:0)
您可以使用绝对文件路径,例如“/ var / www / system / modules / new / $ ad_region”(unix结构)。
或者,例如,如果您的save.php文件位于“system”目录中,并且您想在“system / modules / new /”中创建目录,则可以执行
mkdir("./modules/new/$ad_region");
mkdir有第三个参数,recursive,它允许创建嵌套目录。对于第二个参数,您可以简单地传递0,例如
mkdir("./modules/new/$ad_region", 0, true);