Search code examples
phplinuxwindowspathrequire-once

PHP site runs on linux but not on windows


I developed a website that runs perfectly on Linux, but when I try to run it on the Windows production server, the php script doesn't work. Everything that's in the code after this line doesn't do anything:

require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'returner.php';

I thought that the proble could be related to the paths of the files, so I modified the code to use DIR and DIRECTORY_SEPARATOR to avoid any kind of problems with the paths. I tested if the path was working using file_exists and the file was located, but my code still does nothing when I try to run it on the Windows server.

/index.php includes /lib/returner.php

/lib/returner.php includes /lib/login.php

Code:

index.php:

...
div class="container" role="main">
    <?php
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'returner.php';
        echo '<div class="page-header">';
        echo '<h1>' . returnSede() . '</h1>';
        ...

returner.php:

...
<?php
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'login.php';
    $sede = $_POST['selectSede'];
...

Solution

  • It seems like your code should work.

    Just in case, this answer on Stack Overflow might shed some light. I'll kind of summarize:

    If you're only worried about Unix-based or Windows, hard coding the path with forward slashes ("C:/test.txt") will work.

    There can be discrepancies due to what type of quote you use:

    • for instance, "\123" translates to "Q"
    • Windows requires "C:\\test.txt" or 'C:\test.txt' or "C:/test.txt"

    Another answer on the same question suggests doing something like this:

    define('DS','/');
    define('BASE_ROOT',str_replace('\\',DS,dirname(__FILE__)));
    require_once BASE_ROOT . DS . 'includes' . DS . 'init.php';