Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with PHP includes on case-sensitive file systems

Tags:

php

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 [which does not create problem in Windows, but in Linux]

like image 425
Sourav Avatar asked Jun 12 '26 13:06

Sourav


1 Answers

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.

like image 179
cwallenpoole Avatar answered Jun 15 '26 02:06

cwallenpoole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!