2012-01-25 53 views
1

我正在創建一個內容管理系統,用戶可以通過該界面動態創建類別和部分。它由PHP和MySQL驅動 - 當用戶單擊表單將用戶信息提交給數據庫時,它會動態創建該目錄(如果該目錄尚不存在)以及索引文件(如果該目錄尚不存在)。此外,用戶可以指定與同時創建的部分相對應的特定文件。即使腳本成功,也會生成mkdir()警告

我得到的是我需要的一切:目錄,索引文件和節文件,但我也一直在得到一個mkdir()警告錯誤。警告說:「警告:mkdir()[function.mkdir]:文件存在於..」中,並給出mkdir函數出現的行。我正在使用file_exists()函數來確保目錄和索引文件不存在,但它看起來沒有工作。有任何想法嗎?

我的代碼是:

$dir = $category."/"; 

     if (file_exists($_SERVER['DOCUMENT_ROOT'].$dir)) { 
      chdir($dir); 

      $newFileName = $link_name.".php"; 
      $newFileHandle = fopen($newFileName, 'w+') or die("can't open file"); 
      $category = $_POST['category']; 
      $category = strtoupper($category); 
      fwrite($newFileHandle, implode("\r\n", $content)); 
      fwrite($newFileHandle, '"'.$category.'"'.';'); 
      fwrite($newFileHandle, implode("\r\n", $php_cat_content)); 
      fwrite($newFileHandle, '"'.$section_name.'"'.';'); 
      fwrite($newFileHandle, implode("\r\n", $php_sec_content)); 
      fclose($newFileHandle); 
     } 
     else { 
      $dir = str_replace (" ", "", $category) ."/"; 
      mkdir($dir, 0777); 
      chdir($dir); 

      if (!file_exists("index.php")) { 
      $index_fn = "index.php"; 
      $index_fh = fopen($index_fn, 'w+') or die("can't open file"); 
      $category = $_POST['category']; 
      $category = strtoupper($category); 
      fwrite($index_fh, implode("\r\n", $content)); 
      fwrite($index_fh, '"'.$category.'"'.';'); 
      fwrite($index_fh, implode("\r\n", $php_cat_content)); 
      fwrite($index_fh, '"'.$section_name.'"'.';'); 
      fwrite($index_fh, implode("\r\n", $php_sec_content)); 
      fclose($index_fh); 
      } 

      else { 
      $newFileName = $link_name.".php"; 
      $newFileHandle = fopen($newFileName, 'w+') or die("can't open file"); 
      $category = $_POST['category']; 
      $category = strtoupper($category); 
      fwrite($newFileHandle, implode("\r\n", $content)); 
      fwrite($newFileHandle, '"'.$category.'"'.';'); 
      fwrite($newFileHandle, implode("\r\n", $php_cat_content)); 
      fwrite($newFileHandle, '"'.$section_name.'"'.';'); 
      fwrite($newFileHandle, implode("\r\n", $php_sec_content)); 
      fclose($newFileHandle); 
      } 

回答

0

警告不停止執行腳本。它告訴你,你正在嘗試創建一個已經存在的目錄。在嘗試創建目錄之前,您需要使用is_dir()來檢查目錄是否存在,然後警告就會消失。

您確定$_SERVER['DOCUMENT_ROOT'].$dir準確嗎?在此之下,您正在使用$dir創建目錄只是,但前面沒有任何內容,但是您正在使用文件根目錄檢查目錄的存在。

其餘的代碼工作,因爲目錄確實存在,它不需要被創建。

+0

我換成file_exists()與is_dir()在適當的位置,並得到了相同的,如果不是相似的結果。那是你在想什麼?用is_dir()替換file_exists()? – jbrown574

+0

@ jbrown574:您的文件可能存儲在與您在條件中檢查不同的位置。 – animuson

0

$_SERVER['DOCUMENT_ROOT'].$category."/" and str_replace (" ", "", $category) ."/",你確定它們是一樣的嗎?

0

你需要一個斜槓$_SERVER['DOCUMENT_ROOT']

if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/" . $dir)) { 

應該理清任何問題。

我也注意到這一點可能是缺少$_SERVER['DOCUEMENT_ROOT'] . "/"一起

$dir = str_replace (" ", "", $category) ."/"; 
mkdir($dir, 0777); 

應該是(我認爲)

$dir = $_SERVER['DOCUMENT_ROOT'] . "/" . str_replace (" ", "", $category) ."/"; 
mkdir($dir, 0777); 
+2

我有一種感覺,它可能與絕對路徑有關......謝謝。我會給它一個旋風。就像我說的那樣,它工作,但不是很好。 ; - / – jbrown574

相關問題