Php 即使脚本成功,也会生成mkdir()警告
我正在创建一个内容管理系统,用户可以通过界面动态创建类别和节。它是PHP和MySQL驱动的-当用户单击表单将用户信息提交到数据库时,它会动态创建目录(如果还不存在)和索引文件(如果还不存在)。此外,用户可以指定与正在创建的节相对应的特定文件,该节也同时创建 我得到的是我所需要的一切:目录、索引文件和节文件,但我也得到了一个mkdir()警告错误。警告显示“warning:mkdir()[function.mkdir]:文件存在于..”并给出mkdir函数出现的行。我使用file_exists()函数来确保目录和索引文件不存在,但是,它似乎不起作用。有什么想法吗 我的代码是:Php 即使脚本成功,也会生成mkdir()警告,php,fwrite,mkdir,Php,Fwrite,Mkdir,我正在创建一个内容管理系统,用户可以通过界面动态创建类别和节。它是PHP和MySQL驱动的-当用户单击表单将用户信息提交到数据库时,它会动态创建目录(如果还不存在)和索引文件(如果还不存在)。此外,用户可以指定与正在创建的节相对应的特定文件,该节也同时创建 我得到的是我所需要的一切:目录、索引文件和节文件,但我也得到了一个mkdir()警告错误。警告显示“warning: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);
}
警告不会停止脚本执行。它告诉你你正在尝试创建一个已经存在的目录。在尝试创建目录之前,需要使用检查目录是否存在,然后警告将消失 您确定
$\u服务器['DOCUMENT\u ROOT'.$dir
准确吗?在下面,您只是使用前面没有任何内容的$dir
创建目录,但是您正在使用前面的文档根检查目录的存在性
由于目录确实存在,所以代码的其余部分可以正常工作。$\u服务器['DOCUMENT\u ROOT'].$category./“和
str\u replace(“,”,“,$category)。“/”
,您确定它们是相同的吗?您需要在$\u服务器['DOCUMENT\u ROOT']之后加一个斜杠。
if ( file_exists( $_SERVER['DOCUMENT_ROOT'] . "/" . $dir ) ) {
应该解决所有问题
我还注意到这可能缺少$\u服务器['DOCUEMENT\u ROOT']。“/”
全部放在一起
$dir = str_replace (" ", "", $category) ."/";
mkdir($dir, 0777);
应该是(我想)
我有一种感觉,它可能与绝对路径有关。。。谢谢我要试一试。就像我说的,它很有效,只是不太好-/我在适当的位置用is_dir()替换了文件_exists(),得到了相同的结果(如果不是类似的话)。你是这么想的吗?将文件_exists()替换为is_dir()?@jbrown574:您的文件可能存储在不同的位置,而不是签入您的条件文件。
$dir = $_SERVER['DOCUMENT_ROOT'] . "/" . str_replace (" ", "", $category) ."/";
mkdir($dir, 0777);