首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果php中已经存在,请不要创建目录

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

Stack Overflow用户
提问于 2012-03-25 06:21:09
回答 2查看 3.1K关注 0票数 3

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

代码语言:javascript
复制
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...

如果目录已经存在,脚本是否有可能不创建它?,或者不显示错误?

代码语言:javascript
复制
    <?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..."; 


    } 

    ?>

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

我试过以下几种方法,但还是不满意:-(

代码语言:javascript
复制
            <?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..."; 


            } }

            ?>
EN

回答 2

Stack Overflow用户

发布于 2012-03-25 06:23:33

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

编辑

等等,我是不是在你的错误信息里发现了一个eval

票数 3
EN

Stack Overflow用户

发布于 2012-03-25 06:23:34

您可以结合使用is_dirfile_exists来查看该目录是否存在:

代码语言:javascript
复制
if(file_exists($dirname) && is_dir($dirname)) {
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9856138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档