views:

349

answers:

5

Im new to MVC and I'm re-doing a lot of my code.

I have master.php (index) which has:

<?php require_once("header.php"); ?>

Both master.php and header.php are in the same folder yet I get the following error:

Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in /Users/MyName/Sites/MySite/Views/master.php on line 15

Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.:') in /Users/MyName/Sites/MySite/Views/master.php on line 15

This did not happen before. (Files were all in root folder). I don't see what the problem is though, since they are in the same folder - I don't have to go back '../' or reference any other folders. What am I missing here?

Thanks.

+1  A: 

PHP starts looking for 'header.php' in your root folder and can't find it. If it is in the same directory as the current script, you should rather use the magic constant __DIR__:

<?php require_once __DIR__ . '/header.php'; ?>
soulmerge
Since header.php and master.php are both in the same directory and the include path has '.' in it then this should not be required.
MitMaro
The current directory (include_path = .) is the path from which the script is called, not the directory, the script is residing in.
soulmerge
I get the same exact error.
MrCheetoDust
Then I would first solve the problem referenced in the comments to your question
soulmerge
@soulmerge: How are the calling directory and the directory the script is residing in different?
MitMaro
You can enter the following into a shell for example: php other/dir/file.php. It also happens if 'master.php' is included from a file in another directory: then the current directory is that of the including script, not that of master.php.
soulmerge
+1  A: 

It may be because master.php is being included or required by another script file that is not in the same directory as header.php. That is, when you visit the index, do you know which file is being invoked/parsed?

Take a look at the documentation for include(), which also applies to require():

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/

Update

To address some of the comments: I didn't say that include() would change the CWD. I'm saying that if the CWD is not the one where both master/header.php are located, that could be the issue.

For example, say you have index.php in a different folder than master.php and header.php; it includes or requires master.php using the proper path to it. Now, master.php has the statement:

<?php require_once("header.php"); ?>

However, at this point the CWD does not contain header.php, since the CWD is that of index.php.

To fix this, you need to call require_once with the proper path from the CWD of the current script to header.php, NOT the path from master.php.

Peter
You can get/check/display the current working directory with getcwd(), http://php.net/getcwd . An include()/require() does _not_ change the cwd, only something like chdir() does.
VolkerK
I used getcwd(); and it gave me /Users/MyName/Sites/MySite -- Therefore require_once("Views/header.php"); should work, but it does not. It gives me the same error!
MrCheetoDust
Also, Peter brought up a good point above about master.php being required/included by another script not in the same directory. So I went to http://localhost/~MyName/MySite/Views/master.php and I get the exact same error!
MrCheetoDust
VolkerK, please see my update, I did not say that include or require change the CWD.
Peter
Peter I tried using the CWD (/Users/MyName/Sites/MySite) : require_once("/Users/MyName/Sites/MySite/Views/header.php"); and I get the same error :(.
MrCheetoDust
A: 

You need to have line 15 as this:

<?php (require_once("header.php")) or die(); ?>

However since when header.php is not found the require is fatal then you should have:

<?php (include_once("header.php")) or die(); ?>

or

<?php require_once("header.php"); ?>
MitMaro
+3  A: 

In your comment you say your code is:

<?php require_once("header.php") or die; ?>

please try leaving out the "or die" -> ist is not necessary, because require_once will trigger a fatal error if the file is not found.

Edit: I tested it, leave out the "or die" and it will work.

Edit 2: Explanation why this happened: The above code can also be written like this:

require_once ( ("header.php") or die() );

Because require_once is not a function, but a statement in php, the braces are optional, php interprets the above as a boolean expression, where the string "header.php" gets evaluated to the boolean "true". True gets cast to a string (resulting in "1"), and this string gets passed back to require once.

So the above code gets interpreted as

require_once("1")

Here is how it gets interpreted step by step:

(("header.php") or die()) = (TRUE or die())
(TRUE or die()) = TRUE 
(string) TRUE = '1';

hope this helps ;)

smoove666
MrCheetoDust
Hmm, are you that the header and footer actually have content to show? e.g something gets echo'd? does the page show any php-warnings?
smoove666
I've looked at both header.php and footer.php directly and they both show their content fine.
MrCheetoDust
please put this code before the require_once in you code:ini_set('display_errors', 1);error_reporting(E_ALL | E_STRICT);and tell me if there are any errors / warnings
smoove666
I did it and nothing happened.
MrCheetoDust
Hmm, i'm out of ideas, this kind of stuff is hard to debug without the sourcecode :/
smoove666
header.php is found and includet, else php would show an errormessage. so the problem should be something else.
smoove666
I think you're right smoove666 ... I'm gonna play around with some stuff and report back.
MrCheetoDust
Solved, thanks a bunch sir (and everyone else for contributing, much appreciated) !
MrCheetoDust
A: 

how to call a php file from external folder to internal folder page