2

I am writing a php cli script, and my includes and requires are generating errors.

"PHP Warning: include_once(SCRIPT FOLDER): failed to open stream: Inappropriate ioctl for device in SCIPT PATH on line XX"

Im setting the working directory to the location of the script using

chdir(dirname(__FILE__));

and wrote a wrapper function to include files as such (just code fragments):

$this->_path = rtrim(realpath('./'), '/').'/';     
public function require_file($file)
{ 
  if (include_once $this->_path.$file === FALSE)
    $this->fatal_error('Missing config file (config.php)');
}

What am I doing wrong, or missing?


Answer: (can't answer my own question less than 100 rep)

The proper thing to do when comparing return values from include is

if ((include 'file') === FALSE)

doing it in the wrong fashion will evaluate to include '', causing my error.

3
  • what is the value of $this->_path.$file ? did you log that to check it ? Commented Dec 20, 2011 at 19:04
  • the assignment of the varaible is in the code snippet . . . Commented Dec 20, 2011 at 19:06
  • How can we know which filename you pass to $file ? does the file exists ? is it readable ? ... ? Commented Dec 20, 2011 at 19:11

4 Answers 4

2

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 your include_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.

Sign up to request clarification or add additional context in comments.

4 Comments

actually if include_once can't find the file FALSE is return and and E_WARNING is issued.
My point was that it makes no difference that you get FALSE back, the E_WARNING will still be generated.
I do like your answer, except I'd rather have the error thrown back to the scope that was trying to include the file. php will get out of this terrible procedural error handling into exceptions one day.
@xxxpigeonxxx Well, in that case you should still use a custom error handler, just not inside that function. When the E_WARNING is emitted, your "global" error handler will be invoked and can do the job of generating the fatal error. This obviates the need for your wrapper function entirely.
1

change the ownership of the file to match the one you are including

1 Comment

I made both files im VIM, and I am the owner of both of them.
0

In your if statement you need to put () after your include_once function. Like :

if (include_once($this->_path.$file) === FALSE){ etc.}

2 Comments

alright, i don't know if you may start your variable name with a underscore. Maybe try to echo your path variable the show where it is redirecting it to.
include_once is a language construct and not function, thus the code you provided evaluates to won't work, evaluated as include(($this->_path.$file) == 'OK'), i.e. include('').
0

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;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.