0
$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/ 

目录以便更好地组织。有关如何使其工作的任何帮助?

4

3 回答 3

0

您可以使用相对路径../,例如mkdir("../uploads/".$ad_region)

或使用赦免路径,例如mkdir("/localhost/system/modules/new/".$ad_region)

参考: http: //php.net/manual/en/function.mkdir.php

于 2013-09-24T06:20:59.310 回答
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);
于 2013-09-24T06:25:19.017 回答
0

我要做的第一件事是确保路径在您认为的位置。

试试这个

$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);
}
于 2013-09-24T23:25:55.900 回答