0

I'm trying to guess how to achieve scripts inclusions and keep it throwing errors and warnings. I've got the following code:

if(!@include_once "myscript.php") doSomething();

This will supress "include" warning error if script file doesn't exist and it will include the script file if it exists. The problem comes when an error is generated in "myscript.php". For example, I've got another require_once in "myscript.php" and I caused an error by setting a wrong path. Errors are not displayed and code after that call is not reached/executed.

Is there a way I can supress just the file inclusion errors but not errors generated in the included script? If not, what other way can I use to achieve the target behaviour?

I've thought about using file_exists function to check if script file exists and then include it if TRUE but I'm looking for the simplest way.

I'll put a practical example since it's difficult to understand the actual question:

Imagine that we want to manage a file system that will be used by a user and we need to check whether the user has created a file because that file will be included after the check, but we want to handle the inclusion warning/error so we can show a message or log the warning in a custom log file. First function that comes to our mind is to use file_exists and then include that file. Well, by using @include both functions are performing, it will check if the file exists and it will include the file and that's what I'm researching, the simplest way of doing an action. This way of doing it has an undesired behaviour: it supresses the errors/warnings generated by the included file, not only the inclusion errors/warnings itself. Other reason to avoid file_exists when you are checking if a file exists before trying to include it may be that the way both functions look for a file are different and they may generate different results because of include_path. Is this correct?

14
  • 2
    Using file_exists() is not simple? :P Commented Jan 29, 2018 at 21:09
  • file_exists doesn't work like include / require (stackoverflow.com/questions/2672572/…) Commented Jan 29, 2018 at 21:10
  • can you provide more context about what you are trying to accomplish? typically router style implementations solve this with file_exists check, but an autoloader may also be appropriate. Commented Jan 29, 2018 at 21:11
  • @AkhileshBChandran that would imply to use file_exists and include. It's better to use just one function if possible :D Commented Jan 29, 2018 at 21:12
  • Don't suppress errors. Fix the code so that it doesn't generate them. Commented Jan 29, 2018 at 21:17

1 Answer 1

0

You can use stream_resolve_include_path() to process the filename the way include does, and then check this with file_exists. So:

if (file_exists($path) || file_exists(stream_resolve_include_path($path)) {
    include($path);
}

The first file_exists() call is in case the pathname is absolute, which overrides the include_path setting.

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

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.