开发者

Problem with PHP includes on case-sensitive file systems

开发者 https://www.devze.com 2023-03-15 13:25 出处:网络
I\'ve built a PHP project in Windows, and as you know in Windows fun.php and Fun.php is same but not in Linux.

I've built a PHP project in Windows, and as you know in Windows fun.php and Fun.php is same but not in Linux.

Q1-> As my project will run in Linux How to solve this issue [error for file name] ?

Q2-> I need to show an error if exact file name does not exist ! How to do so ?

Example

I have this 2 PHP file in template.php and render.php

Code of render.php

include 'Template.php';

I want to get an warning so i can fix this issue [whic开发者_StackOverflowh does not create problem in Windows, but in Linux]


Well, if I were in that situation, I'd write a wrapper around include:

function warned_include($file)
{
    if( defined( 'DEBUG_ENV' ) ) // define this somewhere else while in dev.
    {
        foreach( explode( PATH_SEPARATOR, get_include_path() ) as $path )
        {
            // make sure the / is replaced with the OS native DIRECTORY_SEPARATOR
            $fullPath = str_replace($path .DIRECTORY_SEPARATOR.$file, '/', DIRECTORY_SEPARATOR);
            if( is_file( $fullPath ) )
            {
                $spl = new SplFileInfo( $fullPath );
                if( $spl->getPathname() != $fullPath )
                {
                    echo 'case sensitivity mismatch for ' . $spl->getPathname();
                }
                break;
            }
        }
    }
    include $file;
}

I've not had a chance to test it, but SplFileInfo gets the pathname from the OS, which means that it should have correct case sensitivity in Windows.


Solution #1: Use all lowercase. Solution #2: Use the exact same case every time. Instead of trying to include "Template" when the file name is "template", just include the lowercase "template" in the first place. Most programming languages are case sensitive anyway, so it's best to get into the practice. While filename may be unique in this matter, for example, a variable in PHP must use the same case, so just apply the same principals to file names.


You have a 'shortcut' in windows where you call a file Template while it is actually calles template. If you just go trough your code and not allow that, you'd be fine?

  • Make sure all your filenames are in one case, or use one standard (if you just have 'basic' pages, i'd go with all lowercase. But sometimes in bigger projects you'd have a standard that says classes go with a capital like ClassName.php)
  • make sure your calls use that actual name.
  • ...
  • profit.

If for some reason you don't want that, you can always use an autoloader for classes. Every class that is called will load that class if it can't find itself. this will obviously be not ideal for situations where you want to include stuff that is not in classes.


May be you should replace all includes with require. Require throws fatal error if there is no such file exists (not in proper case too)

YOu might find this helpful. - http://clarityamidstchaos.wordpress.com/2008/10/24/how-to-perform-a-case-insensitive-file_exists-lookup-in-phplinux/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号