6

When I try to include a file using include_once in php which shows warning like

Warning: include_once(1) [function.include-once]: failed to open stream: No such file or directory in /var/www/test/content_box.php on line 2

Actually file is there in my directory.I am using ubuntu (OS) How can we prevent this warning?

4
  • (1)? What's that? at least put the extension on that [include_once('1.php')]; Commented Apr 20, 2010 at 4:47
  • 3
    Surely, by 'prevent warning' you mean how to properly include the file, no? Commented Apr 20, 2010 at 4:47
  • 3
    @Ben it is more likely boolean result of some function, being cast to a string :) Commented Apr 20, 2010 at 4:52
  • @OP - You should listen to Col. Shrapnel ^ up here Commented Apr 21, 2010 at 0:37

4 Answers 4

18

Note: My answer below has turned out to be popular, but I didn't really answer the question properly. It's not about suppressing the warning, it's about fixing the bug that caused the warning. I'll leave it here for posterity.


The proper way to prevent the warning would be to check if the first exists first and don't try to include it if it doesn't.

if (file_exists($includefile)) {
  include_once($includefile);
}

(obviously replace $includefile with the file you're including)

The quick and dirty way to do it, which I DO NOT recommend, would be to suppress the warning with the @ expression.

@include_once($includefile);

Note that when suppressing a warning this way, PHP's error handling code still runs but with PHP's error_reporting ini value temporarily changed to ignore it. As a rule of thumb this isn't a good idea because it can mask other errors.

As others have pointed out, "1" is an unusual filename for a PHP file, but I'll assume you have a specific reason for doing that.

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

19 Comments

first one is subject to discuss but second one is terrible
Which is why I used it as an example of what not to do.
Who are you talking to exactly? I can assure you I do understand PHP error handling. Why are you acting as if I was claiming @ to be a good idea? I never claimed that. Using @ can mask other errors in your code, which is why you shouldn't use it. I dunno maybe I'm making the mistake of feeding the trolls here. If you can tell me where you think my answer is wrong, I will fix it. If you just degrade me without pointing out the specifics, then I can't do anything.
@Col. Shrapnel: My point is that you seem to be misreading his original statement. The first four words of the phrase "The quick and dirty way to do it" are logically essential to the subsequent ones. Someone with strong English grammar skils would realize this, and would generally conclude that this was "NOT" a recommendation for any situation where another approach is possible. Perhaps the issue is not so much grammar as idiom comprehension. "Quick and dirty" is generally considered undesirable unless you are desperate.
@Col. Shrapnel: The OP said "prevent this warning". This is ambiguous: it could mean that the goal was to resolve the issue that was creating the warning, or that it was to silence the warning itself. @thomasrutter answered both of the possible interpretations, with the caveat that silencing the warning is not a good idea. In the broader sense, there are some situations where it is useful to silence warnings, so it's good to let people know of this language feature, but important to make sure that it's not misapplied.
|
9

Obviously the issue here is that PHP can't find the file you're trying to include. To reduce confusion about what the current path is, I usually make it an absolute path like this:

// inc.php is in the same directory:
include (dirname(__FILE__) . "/inc.php");

// inc.php is in a subdirectory
include (dirname(__FILE__) . "/folder/inc.php");

// inc.php is in a parent directory
include (dirname(dirname(__FILE__)) . "inc.php");

It's probably a bit over the top, but you can be sure to know where PHP is looking for your files.

1 Comment

Or use __DIR__ if you've got PHP 5.3.
2

that's quite unusual name for the php file - a single digit 1. are you sure you have such a file?

Comments

-1

You're probably trying a statement such as this:

include('header.php') or die('Include file not found.');

In this case what's happening is operator precedence is evaluating it like this:

include ( ('header.php') or die('Include file not found.') );

And because the string 'header.php' does not evaluate to false, it's being cast to a 1 in this context.

You can fix this by rearranging parentheses:

(include 'header.php') or die('Include file not found.');

Note that include is a keyword rather than a function call so no parentheses are required for its argument.

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.