如果已经存在于php中,请不要创建目录

时间:2012-03-24 22:21:09

标签: php directory

我使用以下内容在每个客户的Intranet应用程序上创建一个目录。问题是,如果目录已经存在,我收到错误:

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..."; 


    } 

    ?>

编辑&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;

我尝试了以下内容,但仍然没有喜悦: - (

            <?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 个答案:

答案 0 :(得分:3)

只需使用is_dir和/或file_exists,只有在mkdir返回false时才会调用。

[编辑]

等等,我刚刚在您的错误消息中发现了eval吗?

答案 1 :(得分:2)

您可以将is_dirfile_exists一起使用,以查看该目录是否存在:

if(file_exists($dirname) && is_dir($dirname)) {