File exists but does not include (require_once)

Asked

Viewed 4,056 times

1

I have a lodging (of dubious 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 names in lower case (including folders), so I do one strtolower in function cleanPath.

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 gives error when doing the include, what may be going on?

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

  • I noticed some strange bars in the error: //base/Core\Application is linux the hosting?

  • 1

    You need to make sure that you are not unduly capitalizing and lowercase. According to the output, both the bars, as @rray mentioned, and the marry are mixed, note here: File: Core\Application.class.php and Path: /home/u456897378/base/core/application.class.php. It may even be that PHP is friendly with bars like this: / but the case has no way to work well mixing.

  • Maybe post the function cleanPath help you find the problem.

  • Yes @rray hosting is linux, note that I mount a string to debug and has a line Path: /home/<user>/base/core/application.class.php that’s the string what do I use to make the include.

  • @Bacco added the function, basically just step by string for lowercase and change the bars to /.

  • I don’t understand why /home/<user>/base and then there’s /home/u456897378/base and in error says /home//base. After all, what is the right file path and how is it calling the class to call autoload?

  • I figured out the problem, thank you all... It was just lack of attention.

Show 2 more comments

2 answers

1

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

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

Solution:

if (file_exists($path)){
   $file = $path;
   break;
}

0


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

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

To catch the $path, already here you try to remake the $path without cleaning him up:

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

And that’s why in your answer it worked, it’s probably because you’re using linux, where the files are sensitive when using upper and lower letters, so when using cleanPath worked, because inside of it you use strtolower.

  • Exactly that William, I do not know why Linux is like this, but in Windows you can use UpperCase/com\barras that works properly.

  • @Kaduamaral linux is based on many things in Unix (although both have no connection at all), DOS that wanted to invent new ways rs. Windows only inherited the :p problems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.