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?– rray
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
andPath: /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.– Bacco
Maybe post the function
cleanPath
help you find the problem.– Bacco
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 theinclude
.– KaduAmaral
@Bacco added the function, basically just step by string for lowercase and change the bars to
/
.– KaduAmaral
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?– Daniel Omine
I figured out the problem, thank you all... It was just lack of attention.
– KaduAmaral