PHP case sensitive path's issue

巧了我就是萌 提交于 2019-12-24 16:53:27

问题


I am running a SocialEngine PHP application, which i've recently migrated to another server.

And from that point on - a problem occurs:

The SocialEngine's core trying to include files in case insensitive paths, which appear not to exist (although, in the right case, they do exist)

How can I make the PHP/Apache act nicer, and also search in other cases?

For example, the SocialEngine looks for /application/modules/Menuitems.php, and the right path is /application/modules/Menu**I**tems.php.

To sum up: I want case-insensitive paths!


回答1:


You have case insensitive paths in windows. But when you move your page to linux path will be case sensitive. You can make function for build path that convert all letters to uppercase or lowercase (often preferred) and use one of these normalized naming styles for directory- and file- names.




回答2:


You can experiment with this code I have left only a couple of TODO steps (sorry! have no time.. and it is too big to put it as a comment) you can complete. You put this code at the very begining of the index.php file and every time an include fails the correctFile name is called.

You have to test it separately though as it gets the file name from the warning that php returns. My php version return the file name in parentheses don't know yours. Test the code to see how it works.

<?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'; 
?>

Also the code makes the assumption that the directory part-name is correct! otherwise you have to mnage that too.




回答3:


As already talked about in the comments (I'm sorry if you felt offended, was not meant that way) above and the one given answer there is a fundamental problem:

The filenames the application is using are invalid. They were not on your windows system, but they are on linux.

This is hard to solve. However I had the following idea: PHP StreamWrapper.

Stream Wrappers interoperate between filenames and the underlying code. They enable to access URLs and files with the same interface, e.g. include or file_get_contents.

Normally the type of stream (and onto which you can register your own wrapper) start with something like protocol://.

If no protocol is given, it is file://.

So what you can try is to create your own stream-wrapper that registers onto the file:// protocol. You can then deal with the problems of the case-sensitivity.

The manual provides a pre-made definition of a stream-wrapper-class you might want to re-use.

Others have given hints and code how to resolve case-sensitivity problems. You normally only need to deal with read operations in your case.



来源:https://stackoverflow.com/questions/7948453/php-case-sensitive-paths-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!