我正在用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 14那么到底发生了什么,它还是试图创建它,即使if语句应该阻止它?,我对我做错了什么感到困惑:-S。
<?php
$customerID = $_GET['cfid'];
$directory = "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
$thisdir = getcwd();
mkdir($thisdir ."/customer-files/$customerID" , 0777); }
?>发布于 2012-03-25 07:06:08
替换:
if(file_exists($directory) && is_dir($directory)) {
通过以下方式:
$thisdir = getcwd();
if(file_exists($thisdir.$directory) && is_dir($thisdir.$directory)) {或者更好:
<?php
$customerID = $_GET['cfid'];
$directory = "./customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
}
else {
mkdir($directory , 0777); }
?>发布于 2012-03-25 07:06:47
我只是看了一下,但我会试试这个:
$directory = $thisdir . "/customer-files/$customerID";并从mkdir()中删除$thisdir;
此外,您还应该将$thisdir移到$directory声明之前
发布于 2012-03-25 07:13:41
is_dir()可以使用相对路径,但函数file_exists()不使用相对路径。因此,请使用公分母,并将绝对路径传递给这些函数。此外,您可以将对getcwd()的调用移到$directory赋值中,并在以后重用$directory来创建目录。
<?php
$customerID = $_GET['cfid'];
// Get full path to directory
$directory = getcwd() . "/customer-files/$customerID";
if(file_exists($directory) && is_dir($directory)) {
// Do nothing
}
else {
// Directory doesn't exist, make it
mkdir($directory , 0777); }
}
?>https://stackoverflow.com/questions/9856425
复制相似问题