PHP中如果目录已经存在,则不要创建目录

3
我将使用以下内容为每个客户在内部网络应用程序上创建目录。问题是,如果该目录已经存在,我会收到错误提示:
Warning: mkdir() [function.mkdir]: File exists in     C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo    m_code\custom_code.php(18) : eval()'d code on line 11
 Failed to create directory...

脚本是否可以在目录已存在的情况下不创建该目录?或者不显示错误信息?
    <?php
    $customerID = $_GET['cfid'];

    /* wherever this particular script will be installed, I want to create a subfolder */ 

    /* Step 1. I need to know the absolute path to where I am now, ie where this script is running from...*/ 
    $thisdir = getcwd(); 

    /* Step 2. From this folder, I want to create a subfolder called "myfiles".  Also, I want to try and make this folder world-writable (CHMOD 0777). Tell me if success or failure... */ 

    if(mkdir($thisdir ."/customer-files/$customerID" , 0777)) 
    { 
       echo "Directory has been created successfully..."; 
    } 
    else 
    { 
       echo "Failed to create directory..."; 


    } 

    ?>

编辑 >>>>>>>>>>>>>

我已经尝试了以下方法,但是仍然没有成功 :-(

            <?php
            $customerID = $_GET['cfid'];
            $directory = "/customer-files/$customerID";
            if(file_exists($directory) && is_dir($directory)) { 
            }
            else {
            /* wherever this particular script will be installed, I want to create a subfolder */ 

            /* Step 1. I need to know the absolute path to where I am now, ie where this script is running from...*/ 
            $thisdir = getcwd(); 

            /* Step 2. From this folder, I want to create a subfolder called "myfiles".  Also, I want to try and make this folder world-writable (CHMOD 0777). Tell me if success or failure... */ 

            if(mkdir($thisdir ."/customer-files/$customerID" , 0777)) 
            { 
               echo "Directory has been created successfully..."; 
            } 
            else 
            { 
               echo "Failed to create directory..."; 


            } }

            ?>
2个回答

3

只需使用is_dir和/或file_exists,并且仅在它们返回false时才调用mkdir

[编辑]

等等,我刚刚在你的错误消息中发现了一个eval


我也是。在PHP中,何时使用“_”完全是随机的。 - GolezTrol

2
你可以使用is_dirfile_exists来判断目录是否存在:
if(file_exists($dirname) && is_dir($dirname)) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接