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条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助