PHP区分大小写的路径问题

时间:2011-10-30 22:45:08

标签: php path case-insensitive socialengine

我正在运行一个SocialEngine PHP应用程序,我最近已将其迁移到另一台服务器。

从那时起 - 出现问题:

SocialEngine的核心尝试将文件包含在不区分大小写的路径中, 似乎不存在(虽然,在正确的情况下,它们确实存在)

如何让PHP / Apache更好,并在其他情况下搜索?

例如,SocialEngine查找/application/modules/Menuitems.php, 正确的路径是/application/modules/Menu**I**tems.php

总结一下:我想要不区分大小写的路径!

3 个答案:

答案 0 :(得分:4)

Windows中有不区分大小写的路径。但是,当您将页面移动到Linux路径时,将区分大小写。您可以为构建路径创建函数,将所有字母转换为大写或小写(通常是首选),并使用这些规范化命名样式之一作为目录和文件名。

答案 1 :(得分:0)

您可以尝试使用此代码我只留下了几个TODO步骤(抱歉!没时间......而且它太大了,无法将其作为注释)您可以完成。您将此代码放在index.php文件的最开头,每次包含失败时都会调用correctFile名称。

你必须单独测试它,因为它从php返回的警告中获取文件名。我的php版本在括号中返回文件名不知道你的。测试代码以查看其工作原理。

<?PHP
/*****************put this at the very top*********************************/
 $flag=2; 
 function correctFileName($incorrectFileName)
 {
    //directory and file to look at/for
    $dirToLookAt = dirname($incorrectFileName);
    $fileToLookFor = basename($incorrectFileName);

    //get all .php files
    $files = array(); $directory = opendir($dirToLookAt ); 
    while($item = readdir($directory)) 
        if(is_string(strstr($item,'.php')))
            $files[] = $item; 

    //to do
    /*
    loop through the $files array and to 
    a case insensitive search for $incorrectFileName

    if you find one then rename the file you found to $incorrectFileName

    then break the loop
    */

 }
//error handler function
function customError($errno, $errstr)
  {
        global $flag;
        //find the file name
        if($errno==2 and (($flag%2)==0))
        {
              //this will allow to enter only once per time
              $flag++;
              //get the file name
             $start = strpos($errstr, '(') +1;//7
             $length = strpos($errstr, ')') -  $start ;//10
             correctFileName(substr($errstr, $start  ,$length));
        }
  }

//set error handler
set_error_handler("customError");
/*****************************************************************/

//trigger error
include 'c:\www\home\122221.php'; 
?>

此外,代码假设目录部件名称是正确的!否则你也必须这样做。

答案 2 :(得分:0)

正如在评论中已经谈到的那样(我很抱歉,如果你感到被冒犯,并不是那种意思)上面和the one given answer有一个根本问题:

应用程序使用的文件名无效。它们不在您的Windows系统上,但它们在Linux上。

这很难解决。但是我有以下想法:PHP StreamWrapper

Stream Wrappers在文件名和底层代码之间进行互操作。它们允许访问具有相同接口的URL和文件,例如includefile_get_contents

通常,流的类型(以及您可以在其上注册自己的包装器)从protocol://开始。

如果没有给出协议,则为file://

因此,您可以尝试在file://协议上创建自己的registers流包装器。然后,您可以处理区分大小写的问题。

本手册提供了您可能想要重复使用的pre-made definition of a stream-wrapper-class

其他人已经提供了如何解决区分大小写问题的提示和代码。您通常只需要处理您的情况下的读取操作。