以下是我的代码
<?php
$folder_type=$_POST['folder_type'];
$folder_name=$_POST['folder_name'];
$images="images";
$path="../../".$folder_type."/".$folder_name;
if (!is_dir("../../".$folder_type."/".$folder_name)) {
mkdir("../../".$folder_type."/".$folder_name);
mkdir("../../".$folder_type."/".$folder_name."/".$images);
$content = file_get_contents('../../default_code.php');
$fp = fopen($path . "/$folder_name.php","wb");
fwrite($fp,$content);
fclose($fp);
}
else
{
echo "0";
}
chmod("../../".$folder_type."/".$folder_name, 0777);
?>
如果我的目录中有名为ALTO的foldername,并且如果我尝试使用alto的新文件夹名称,那么它就不会接受bt is_dir()函数不会使用casesensitive进行检查。如果我尝试使用ALTO,它就不会接受。那么有没有其他方法来检查我的新foldername是否已经存在于目录中?
请帮助任何人。提前谢谢。
答案 0 :(得分:0)
我个人喜欢避免文件夹和文件名中的任何大写字符。我建议在应用之前始终在文件夹/文件名上使用strtolower()。通过这种方式,您的支票也可以随时使用。
<?php
$folder_type = strtolower($_POST['folder_type']);
$folder_name = strtolower($_POST['folder_name']);
$images = "images";
$path = "../../" . $folder_type . "/" . $folder_name;
if (!is_dir("../../" . $folder_type . "/" . $folder_name)) {
mkdir("../../" . $folder_type . "/" . $folder_name);
mkdir("../../" . $folder_type . "/" . $folder_name . "/" . $images);
$content = file_get_contents('../../default_code.php');
$fp = fopen($path . "/$folder_name.php", "wb");
fwrite($fp, $content);
fclose($fp);
} else {
echo "0";
}
chmod("../../" . $folder_type . "/" . $folder_name, 0777);
?>
或者,如果您真的想继续使用大写的名称,您可以通过首先获取所有文件夹和文件的列表,循环遍历它们然后按文件名检查来更改if条件以检查大写和小写名称。然而,这将是低效的,我认为不惜任何代价保持大写名称没有优势。
修改强>
如果您不想手动将任何现有文件夹和文件编辑为小写,则可以执行以下操作:
<?php
/**
* Check for folder existence by name, case insensitive
* @param $folder_name
* @param string $dir
* @return array
*/
function folder_exists($folder_name, $dir = '*') {
// get all folders in provided dir
$folders = array_filter(glob($dir), 'is_dir');
//now do a case insensitive comparison
return preg_grep( "/" . $folder_name . "/i" , $folders );
}
$folder_type = strtolower($_POST['folder_type']);
$folder_name = strtolower($_POST['folder_name']);
$images = "images";
$path = "../../" . $folder_type . "/" . $folder_name;
if (!folder_exists($folder_type, "../../")){
mkdir("../../" . $folder_type);
}
if(!folder_exists($folder_name, "../../" . $folder_type)) {
mkdir($path);
mkdir($path . "/" . $images);
$content = file_get_contents('../../default_code.php');
$fp = fopen($path . "/$folder_name.php", "wb");
fwrite($fp, $content);
fclose($fp);
} else {
echo "0";
}
chmod("../../" . $folder_type . "/" . $folder_name, 0777);
?>
答案 1 :(得分:0)
您可以使用strtolower
,因此它会创建小写文件夹,您无需担心目录检查
$folder_type=$_POST['folder_type'];
$folder_name=$_POST['folder_name'];
$folder_type = strtolower( $folder_type );
$folder_name = strtolower( $folder_name );
$images="images";
$path="../../".$folder_type."/".$folder_name;
if (!is_dir("../../".$folder_type."/".$folder_name))
{
mkdir("../../".$folder_type."/".$folder_name);
mkdir("../../".$folder_type."/".$folder_name."/".$images);
$content = file_get_contents('../../default_code.php');
$fp = fopen($path . "/$folder_name.php","wb");
fwrite($fp,$content);
fclose($fp);
}
else
{
echo "0";
}
chmod("../../".$folder_type."/".$folder_name, 0777);