PHP warning when using include_once ()

When I try to include a file using include_once in php that shows a warning like Warning: include_once (1) [function.include-once]: stream failed to open: there is no such file or directory in / var / www / test / content _box .php on line 2. Typically, the file is in my directory. I am using ubuntu (OS) How can we prevent this warning. If anyone knows please help me

+5
source share
4 answers

Note My answer below turned out to be popular, but I did not answer the question very correctly. This is not about suppressing a warning, but about fixing the error that caused the warning. I will leave it here for posterity.


The correct way to prevent a warning would be to check if the first exists, and not try to turn it on if it is not.

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

(obviously replace $ includefile with the file you included)

A quick and dirty way to do this, which I recommend NOT recommending , is to suppress the warning using the @ expression.

 @include_once($includefile); 

Note that while suppressing the warning, this method still works with the PHP error handling code, but with the modified PHP value, error_reporting ini is temporarily ignored. This is generally not a good idea, as it may mask other errors.

As others have noted, "1" is an unusual file name for a PHP file, but I assume that you have a specific reason for this.

+15
source

Obviously, the problem here is that PHP cannot find the file you are trying to include. To reduce confusion about the current path, I usually do this in an absolute way as follows:

 // 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"); 

This is probably a little higher, but you can be sure that PHP is looking for your files.

+8
source

this is a rather unusual name for the php file - one digit 1 . Are you sure you have such a file?

+2
source

You are probably trying to make this statement:

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

In this case, the operator has priority, evaluating it as follows:

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

And since the string 'header.php' not evaluated as false, in this context it is cast to 1 .

You can fix this by changing the brackets:

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

Note that include is a keyword, not a function call, so brackets are not required for its argument.

0
source

Source: https://habr.com/ru/post/1307416/


All Articles