2012-03-24 109 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)) { 
相關問題