【问题标题】:is_dir () function is case sensitiveis_dir() 函数区分大小写
【发布时间】:2016-11-08 08:53:33
【问题描述】:

下面是我的代码

<?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,并且如果我尝试使用 alto 的新文件夹名称,它将不接受正确的 bt is_dir() 函数不检查大小写。如果我尝试使用 ALTO,它不会接受。那么有没有其他方法可以检查我的新文件夹名是否已经存在于目录中?

请帮助任何人。提前致谢。

【问题讨论】:

  • 区分大小写的原因仅仅是服务器系统上通常使用的文件系统是区分大小写的。 unixoid 环境中的文件系统一直是区分大小写的。只有 MS-Windows 的行为不同,在 OSX 环境中,这取决于配置。
  • 解决您的情况的最佳方法就是遵守纪律并坚持使用小写字母作为文件名。这样,事情就很明确了,因此是可移植的。因此,您可以在环境之间移动您的项目或应用程序,而不会破坏一切。
  • 那么我们该如何处理这个问题
  • @arkascha 真的,OSX 有一个配置选项吗?我从来不知道。
  • strtolower() 我将尝试比较它现在的工作情况

标签: php file directory


【解决方案1】:

你可以使用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);

【讨论】:

  • 如果文件夹名称是"FooBar" !== "foobar",这将不起作用。现在,如果您要从那里获取该信息,可以说scandir()..
  • 是的。但是以后不会创建重复目录
  • 我尝试了这部分,但是当我在服务器中尝试时,它会自动创建同名文件夹
【解决方案2】:

我个人喜欢在我的文件夹和文件名中避免使用任何大写字符。我建议在应用之前始终在文件夹/文件名上使用 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);

?>

【讨论】:

  • 如果文件夹名称是“FooBar”,这将不起作用!==“foobar” 现在,如果您要从那里获取该信息,可以说 scandir()..
  • 如果文件夹在实施时为空,它会。 FooBar 一开始就不会被创建~
  • 我尝试了这部分,但是当我在服务器中尝试时,它会自动创建同名文件夹
  • 你能说得更具体点吗?你尝试了什么,创造了什么?
  • 我尝试创建名称为 figo 的文件夹,但该目录已经具有带有FIGO 的文件夹名称,因此它不会创建新文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
相关资源
最近更新 更多