即使脚本成功,也会生成 mkdir() 警告
我正在创建一个内容管理系统,用户可以通过界面动态创建类别和部分。它是 PHP 和 MySQL 驱动的 - 当用户单击表单将用户信息提交到数据库时,它会动态创建目录(如果尚不存在)和索引文件(如果尚不存在)。另外,用户可以指定与正在创建的节相对应的特定文件,该文件也同时创建。
我得到的是我需要的一切:目录、索引文件和节文件,但我也收到了 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);
}
I'm creating a content management system whereby the user can create categories and sections dynamically through an interface. It's PHP and MySQL driven - when the user clicks the form to submit the user info to the database, it dynamically creates the directory (if it doesn't already exist) and an index file (if it doesn't already exist.) In addition, the user can specify a specific file corresponding to the section being created that is also created at the same time.
What I've been getting is everything I need: the directory, the index file and the section file but I've also been getting a mkdir() warning error. The warning says "Warning: mkdir() [function.mkdir]: File exists in.." and gives the line that the mkdir function appears on. I'm using the file_exists() function to make sure that the directory and index file don't already exist, however, it doesn't appear to be working. Any ideas?
My code is:
$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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
警告不会停止脚本执行。它告诉您正在尝试创建一个已经存在的目录。在尝试之前,您需要使用
is_dir()
检查该目录是否存在创建它,然后警告就会消失。您确定
$_SERVER['DOCUMENT_ROOT'].$dir
准确吗?在下面,您使用$dir
创建目录just,前面没有任何内容,但您正在检查该目录是否存在,并在其前面有一个文档根目录。其余代码可以正常工作,因为目录确实存在,不需要创建它。
A warning doesn't stop script execution. It's telling you that you're trying to create a directory that already exists. You need to use
is_dir()
to check if the directory exists or not before you attempt to create it, then the warning will go away.Are you sure
$_SERVER['DOCUMENT_ROOT'].$dir
is accurate? Below that, you're creating the directory just using$dir
with nothing on the front, but you're checking the directory's existence with a document root on the front of it.The rest of your code works because the directory does exist, it didn't need to be created.
$_SERVER['DOCUMENT_ROOT'].$category."/"
和str_replace (" ", "", $category) ."/"
,您确定它们是相同的?$_SERVER['DOCUMENT_ROOT'].$category."/"
andstr_replace (" ", "", $category) ."/"
, are you sure they are same?$_SERVER['DOCUMENT_ROOT']
后面需要有一个斜杠。应该解决任何问题。
我还注意到这可能缺少
$_SERVER['DOCUMENT_ROOT'] 。 “/”
全部在一起应该是(我认为)
You need a slash after
$_SERVER['DOCUMENT_ROOT']
.Should sort out any problems.
I also notice this is probably missing the
$_SERVER['DOCUEMENT_ROOT'] . "/"
all togethershould be ( I think )