File exists but does not include (require_once)

1

I have a hosting (of doubtful quality), I am making a require_once where I check if the file exists, and is giving error:

  

Warning: require_once (/home//base/Core\Application.class.php): failed to open stream: No such file or directory in /home//base/config/autoload.php on line 106

     

Fatal error: require_once (): Failed opening required '/home//base/Core\Application.class.php' (include_path = '.: / opt / php-5.5 / pear') in / home // base /config/autoload.php on line 106

My Autoload:

function cleanPath($lib, $file, $ds = '/') {
    $lib = rtrim($lib, '/\');

    $path = strtolower($lib.$ds.$file);
    $path = str_replace(['\', '/'], $ds, $path);

    return $path;
}

spl_autoload_register(
    function ($class){
         global $autoloadlog;

         $libs = [BASE, APPS.APP.DS];
         $ext  = '.class.php';
         $debug = !TRUE;

         $file = FALSE;

         $autoloadlog .= '<h3>'.$class.'</h3>';

         foreach ($libs as $lib) {
            $path = cleanPath($lib, $class.$ext, DIRECTORY_SEPARATOR);

             $autoloadlog .= '<pre>Lib: ' . (is_array($lib) ? implode(', ', $lib) : $lib) . PHP_EOL . 
                            'File: ' . $class.$ext . PHP_EOL . PHP_EOL . 
                            'Path: ' . $path . PHP_EOL . PHP_EOL . 
                            (file_exists($lib.$class.$ext) ? 'EXISTS!' : 'NOT exists!') . PHP_EOL . 
                            'BackTrace: ' . var_export(debug_backtrace(), true) . PHP_EOL . PHP_EOL . 
                            str_repeat('-', 200) . PHP_EOL . PHP_EOL.'</pre>';


             if (file_exists($path)){
                  $file = $lib.$class.$ext;
                  break;
             }
         }

         //$file = search_lib($libs, $class.$ext);

         // Debug
         if ($debug) echo $autoloadlog;

         // Se encontrou inclui o arquivo
         if ($file !== FALSE  && is_string($file) && $file !== '') {

             require_once $file;

             if (!class_exists($class, FALSE)){
                  trigger_error('Autoload error: File loaded, but class not found.' , E_USER_ERROR);
                  //throw new \Core\Exception\SystemException(\Core\Exception\Exceptions::E_CLASSNOTEXIST, [$class]);
             }

         } else { // Se não encontrar o arquivo lança um erro na tela. :)

             if (is_array($libs)) $libs = implode($class.$ext . '</code>, <code>', $libs);

             trigger_error("Autoload error: Can't find the file {$class}{$ext} on [{$libs}]!" , E_USER_ERROR);
             //throw new \Core\Exception\SystemException(\Core\Exception\Exceptions::E_FILENOTFOUND, ["<code>{$libs}".$class.$ext."</code>"]);
         }

    }
);

Note: Files are all saved with lowercase names (including folders), so I make a strtolower in the cleanPath function.

Return:

<h3>Core\Application</h3><pre>Lib: /home/u456897378/base/
File: Core\Application.class.php

Path: /home/<user>/base/core/application.class.php

NOT exists!
BackTrace: array (
  0 => 
  array (
    'function' => '{closure}',
    'args' => 
    array (
      0 => 'Core\Application',
    ),
  ),
  1 => 
  array (
    'file' => '/home/<user>/public_html/index.php',
    'line' => 8,
    'function' => 'spl_autoload_call',
    'args' => 
    array (
      0 => 'Core\Application',
    ),
  ),
)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</pre><br />
<b>Warning</b>:  require_once(/home/<user>/base/Core\Application.class.php): failed to open stream: No such file or directory in <b>/home/<user>/base/config/autoload.php</b> on line <b>106</b><br />
<br />
<b>Fatal error</b>:  require_once(): Failed opening required '/home/<user>/base/Core\Application.class.php' (include_path='.:/opt/php-5.5/pear') in <b>/home/<user>/base/config/autoload.php</b> on line <b>106</b><br />

In short, the file exists, but it does give error when doing include , what could be happening?

Note: It works correctly in local environment (XAMPP + Windows).

    
asked by anonymous 06.11.2015 / 22:17

2 answers

0

I know that you have solved the problem, but I just wanted to point out the possible problem, here you use:

$path = cleanPath($lib, $class.$ext, DIRECTORY_SEPARATOR);

To get the $path , already here you try to redo $path without cleaning it:

 $file = $lib.$class.$ext;
 break;

And because of this in your answer worked, this was probably due to you being using linux, where the files are sensitive when using lowercase and lowercase letters, so when using cleanPath it worked because inside it you use strtolower .

    
09.11.2015 / 11:50
1

I used a string to check if the file exists, but I used another to do require :

if (file_exists($path)){
   $file = $lib.$class.$ext;
   break;
}

Solution:

if (file_exists($path)){
   $file = $path;
   break;
}
    
09.11.2015 / 11:12