command line interface - php cli include_once error

Solution:
Well,include_once
is a special language construct, not a function. As such you shouldn't try to use a return value from it (like === FALSE). The PHP manual entry on the topic says that "The include() construct will emit a warning if it cannot find a file" so, checking that === FALSE doesn't help your situation.
My recommendation would be to use a custom error handler that throws exceptions when PHP errors are raised. Then you could wrap yourinclude_once
in a try/catch block to handle the exception caused by the invalid include however you like.
So, for example ...
function require_file($file)
{
set_error_handler(function($errno, $errstr) { throw new Exception($errstr); });
try {
include_once $file;
restore_error_handler();
echo 'woot!';
} catch (Exception $e) {
echo 'doh!';
}
}
$file = 'invalid_filename.php';
require_file($file); // outputs: doh!
Note: I use a closure in this example. If you're working with < PHP5.3 you'd need to use an actual function for the error handler.
Answer
Solution:
change the ownership of the file to match the one you are including
Answer
Solution:
In your if statement you need to put()
after your include_once function. Like :
if (include_once($this->_path.$file) === FALSE){ etc.}
Answer
Solution:
Another way to solve your problem is to test the readibility of the file you want to include, in your wraper.
$this->_path = rtrim(realpath('./'), '/').'/';
public function require_file($file)
{
$pathFile = $this->_path . $file;
if ( ! is_readable($pathFile)) {
$this->fatal_error('Missing file (' . $file . ')');
} else {
include_once $pathFile;
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.