简体   繁体   English

PHP的问题包括区分大小写的文件系统

[英]Problem with PHP includes on case-sensitive file systems

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. 我已经在Windows中构建了一个PHP项目,正如您在Windows中所知,fun.php和Fun.php是相同的但不是在Linux中。
Q1-> As my project will run in Linux How to solve this issue [error for file name] ? Q1->因为我的项目将在Linux中运行如何解决此问题[文件名错误]?
Q2-> I need to show an error if exact file name does not exist ! Q2->如果确切的文件名不存在,我需要显示错误! How to do so ? 怎么办?

Example

I have this 2 PHP file in template.php and render.php 我在template.phprender.php有这个2 PHP文件
Code of render.php render.php代码

include 'Template.php';

I want to get an warning so i can fix this issue [which does not create problem in Windows, but in Linux] 我想得到一个警告,所以我可以解决这个问题[这不会在Windows中产生问题,但在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. 我没有机会测试它,但是SplFileInfo从操作系统获取路径名,这意味着它应该在Windows中具有正确的区分大小写。

Solution #1: Use all lowercase. 解决方案#1:全部使用小写。 Solution #2: Use the exact same case every time. 解决方案#2:每次使用完全相同的情况。 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. 虽然文件名在这个问题上可能是唯一的,例如,PHP中的变量必须使用相同的大小写,因此只需将相同的主语应用于文件名即可。

You have a 'shortcut' in windows where you call a file Template while it is actually calles template . 您在Windows中有一个“快捷方式”,您可以在其中调用文件Template而实际上是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) 确保所有文件名都在一个案例中,或者使用一个标准(如果你只有'基本'页面,我会全部用小写字母。但有时候在更大的项目中你有一个标准,说课程用一个资本像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替换所有包含。 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/ - http://clarityamidstchaos.wordpress.com/2008/10/24/how-to-perform-a-case-insensitive-file_exists-lookup-in-phplinux/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM