我正在創建一個內容管理系統,用戶可以通過該界面動態創建類別和部分。它由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);
}
我換成file_exists()與is_dir()在適當的位置,並得到了相同的,如果不是相似的結果。那是你在想什麼?用is_dir()替換file_exists()? – jbrown574
@ jbrown574:您的文件可能存儲在與您在條件中檢查不同的位置。 – animuson