2012-03-24 76 views
3

我正在使用以下命令为每个客户在Intranet应用程序上创建一个目录。问题是,如果该目录已经存在,我得到的错误:如果在php中已经存在,则不要创建目录

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


      } } 

      ?> 

回答

3

只需使用is_dir和/或file_exists,只有打电话mkdir如果他们返回false。

[编辑]

等等,我在错误信息中发现了eval

+0

你的意思是MKDIR,我猜。 – Luci 2012-05-02 11:35:59

+0

我做到了。在PHP中使用'_'时完全随机。 – GolezTrol 2012-05-02 11:37:25

2

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

if(file_exists($dirname) && is_dir($dirname)) { 
相关问题