dongpaipu8394 2013-09-24 06:09
浏览 326
已采纳

使用mkdir(),未创建目录

$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!";
}

I am making a site and I am developing it in localhost for now. My save.php file and the uploads folders where the codes above is saved in the local directory

localhost/system/modules/new/

When I relocated the save.php file and the uploads folder in the directory

localhost/system/

all seems to be working now. But I want it to work in the

localhost/system/modules/new/ 

directory for better organization. Any help on how to make it work?

  • 写回答

3条回答 默认 最新

  • dongqie7806 2013-09-24 23:25
    关注

    First thing I'd do is ensure that the paths are where you think they are.

    Try this out

    $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能