phpphp-5.6php-7.2include-once

Error with PHP 5.6 to PHP 7.2.4. Include_once don´t work


The video illustrating the problem in real time is available here:

link: https://www.youtube.com/watch?v=IaTJNS31w6A&feature=youtu.be

I have been learning web-development. Newbie in PHP.

I created a site in a local server WAMP. By default WAMP uses PHP 5.6. When the version is changed to 7.2.4, the site doesn't work. The warnings are:

Warning: include_once(/app/config.inc.php): failed to open stream: No such file or directory in E:\wamp64\www\ziurfreelance2\index.php on line 3

and

Warning: include_once(): Failed opening '/app/config.inc.php' for inclusion (include_path='.;/path/to/php/pear/.;') in E:\wamp64\www\ziurfreelance2\index.php on line 3

I changed the include_path in php.ini, just trying to find a solution. It didn't work, though.

The files are correctly placed (in the video you can see the site works just fine) and nothing more is done but change the PHP versions.

The files are correctly placed (in the video you can see the site works just fine), only the PHP versions are different.

The project' tree here: https://i.pinimg.com/originals/c9/52/68/c9526844ba7a80ba63baeaccab1965e7.png

i really hope you can help me. Maybe, as I readed in some place, the bug is related to apache, that info was not very clear and specific.

Even defining a constant in the same file (note that the include_once calls the config.inc.php where all the other used constants are) and maybe it works, but Why does the version break it?

Here's the index.php (placed in the root directory):

<?php

include_once "/app/config.inc.php";

$componentes_url = parse_url($_SERVER["REQUEST_URI"]);
$ruta = $componentes_url['path'];
$partes_ruta = explode("/", $ruta);
$partes_ruta = array_filter($partes_ruta);
$partes_ruta = array_slice($partes_ruta,0);

$ruta_elegida = '/paginas/404.php';


if($partes_ruta == null){

$ruta_elegida = '/paginas/home.php';

}

//para subpaginas de primer nivel

else if(count($partes_ruta) == 1){

    switch($partes_ruta[0]){

        case 'aviso-legal';
            $ruta_elegida = '/paginas/aviso-legal.php';
            break;

        case 'sobre-mi';
            $ruta_elegida = '/paginas/bio.php';
            break;

        case 'blog';
            $ruta_elegida = '/paginas/blog.php';
            break;

        case 'contacto';
            $ruta_elegida = '/paginas/contacto.php';
            break;

        case 'diseno-grafico';
            $ruta_elegida = '/paginas/diseno-grafico.php';
            break;

        case 'diseno-web';
            $ruta_elegida = '/paginas/diseno-web.php';
            break;

        case 'fotografia';
            $ruta_elegida = '/paginas/fotografia.php';
            break;

        case 'modelado';
            $ruta_elegida = '/paginas/modelado.php';
            break;

        case 'video-y-animacion';
            $ruta_elegida = '/paginas/video-y-animacion.php';
            break;

        case 'ziur11235813213455fotos';
            $ruta_elegida = '/paginas/subida-fotos.php';
            break;
        }

}

include_once $ruta_elegida;

And here the .htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php

Thx for your time.


Solution

  • You should use __DIR__ magic constant to generate correct absolute paths for files. For example:

    include_once __DIR__ . '/../app/config.inc.php';
    

    getcwd() may give you unexpected results - it will return different value for these 2 calls:

    php index.php
    php myapp/index.php
    

    since you're calling index.php from two different places. __DIR__ will always return the same value regardless of place from you're running PHP script.