Reputation: 12867
I make tests on localhost (Windows, WAMP Server) so never bothered to test my code again after pushing them to remote server (Linux) which, after a few days, is giving this error:
failed to open stream: No such file or directory
So I looked into it and found something.
Here is the crime scene and my detective work on localhost:
echo $initEntityAutoFile;
if (file_exists($initEntityAutoFile)) {
echo ' - EXISTS - ';
} else {
echo ' - NOT - ';
}
$initEntityAuto = require $initEntityAutoFile; // the line giving the error
echo get_class($initEntityAuto);
echo PHP_EOL, get_include_path();
Which gives this:
/cli/aaa/bbb/ccc.php - NOT - Closure
.;C:\php\pear
Seems I'm requiring this path: /cli/aaa/bbb/ccc.php
which is a relative path immediately under my project document root. No wonder my remote server doesn't find the file since it's not an absolute path at all. It should be /home/xxx/public_html/cli/aaa/bbb/ccc.php
or C:/project/cli/aaa/bbb/ccc.php
.
But the weird thing is, while the remote server gives an error on this, my localhost is correctly finding the file - with file_exists() returning FALSE but $initEntityAuto
successfully getting the content, a closure in that file. Neither does the require
raise any error, otherwise I would have found the problem and fixed it.
This is really weird. Why?
I could just correct the path and get on but this is kind of haunting me. If anybody can give an answer, I would sleep better.
===========
Additional info:
require
line is in a function in a file that's very deep down the project directories, so it's not in the same directory as /cli/aaa/bbb/ccc.php
.$initEntityAutoFile
, a.k.a. the path string /cli/aaa/bbb/ccc.php
is passed in the function as a parameter.Upvotes: 1
Views: 50
Reputation: 12867
As suggested by @Alexey, I used getcwd() to find what the current working directory is before the require
line:
- localhost: X:\my_project
- remote server: /home/xxx/public_html
Now that I'm requiring this path: /cli/aaa/bbb/ccc.php
(with a beginning slash), on my localhost (Windows, WAMP Server), the path is automatically translated to absolute path:
X:\my_project/cli/aaa/bbb/ccc.php
So my code works properly on localhost.
However on the remote server (Linux), when I'm requiring the path /cli/aaa/bbb/ccc.php
, the file system treats it as an absolute path instead of concatenating it to the current working directory because it has a beginning slash:
/cli/aaa/bbb/ccc.php
Therefore causing the "failed to open stream: No such file or directory" error.
Eventually, we have 2 solutions:
Upvotes: 1