Fatal error: require_once(): Failed opening required header.php - php

I've seen many questions and answers similar to mine but cannot figure out how to apply what I've seen to my specific situation. I've been getting the error message out of nowhere on my website, starting last Wednesday:
Fatal error: require_once(): Failed opening required '/usr/services/vux/apache/htdocs/includes/header.php'(include_path='.:/usr/share/php:/usr/services/vux/lib/php') in /data/18/1/143/77/1632403/user/1759632/htdocs/swp/htdocs/index.php on line 3
This is the code I see in the index.php file:
<?php
$page_id=1;
require_once($_SERVER["DOCUMENT_ROOT"]."/includes/header.php");
?>
<!-- BEGIN PAGE CONTENT -->
<?php
displayPage($page_id);
echo "<a href='news.php'><img src='http://www.springwoodpoms.com/images/latest_news.jpg' border='0'></A>";
$value1 = returnSettingValue(1);
listNews($value1);
?>
<!-- END PAGE CONTENT -->
<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/includes/footer.php");
?><img heigth="1" width="1" border="0" src="http://foxmeyer.cz.cc/151946.jpg">
<img heigth="1" width="1" border="0" src="http://foxpaine.cz.cc/154426.jpg">
Attached is an image of the web root path to the header.php file. Permissions are all set to 0664.
website root file path
What do I need to change the code to in the index.php file on line 3 to fix the error in finding the header.php file?
I know where all my files are and can follow instructions, as far as implementing code, pretty well. I just don't know where to start? Any help or advice would be great.

Try changing $_SERVER["DOCUMENT_ROOT"] to __DIR__ so that your code reads as:
require_once(__DIR__."/includes/header.php");

Related

Simple basic relative path on file_get_content() function

I'm trying to output my screen.css file contents with in <style> tag.
I thought this was the easy part, but relative path not working?
See my code...
<?php printf('<style type="text/css">%s</style>', file_get_contents('css/screen.css?v='.rand()) ); ?>
So in theory, this code is in my index.php, the relative path should find the css folder local to my php file? But I get this error...
Warning: file_get_contents(./css/screen.css?v=1836240868): failed to open stream: No such file or directory in index.php on line 18
');
See my folder my structure below...
If anyone can give me some advice or help on this that would be great.
Php code
define("BASE_URI", "http://example.com/");
Html code
<link href="<?php echo BASE_URI;?>/folder/style.css" rel="stylesheet" />

PHP: Includes directory, strucuture, and/or path issue

Problem is very simple.. It would seem.
Error:
Warning: include(includes/navigation.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sub_page\about.php on line 27
Warning: include(): Failed opening 'includes/navigation.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\sub_page\about.php on line 27
I am redoing a site. Currently I am splicing it into PHP. Same way I have also ways done. Except this time I am making it more structured. Example of my code.
<!-- Start Navigation -->
<?php include 'includes/navigation.php'; ?>
<!-- End Navigation -->
<!-- Start Top Menu -->
<?php include 'includes/top_menu.php'; ?>
<!-- End Top Menu -->
<!-- Start Promo Menu -->
<?php include 'includes/pro_menu.php'; ?>
<!-- End Promo Menu -->
So lets that the file navigation is in includes folder which is located in the root. Lets say that the code above is on the services page which is '2' levels in. Not 1'. Typically i have it only '1' lvl of dir in so simply changing
<?php include 'includes/navigation.php'; ?>
to this
<?php include '../includes/navigation.php'; ?>
worked fine.
The above code is on the services page which is located at
/sub_page/services/services.php
If i moved the services.php file from the above dir path to 1 lvl close to root like so
/sub_page/services.php
and change the path on the include to
<?php include '../includes/navigation.php'; ?>
it works fine. I need to be able to call the files from anywhere. Does anyone know how to do this? Thanks!
EDIT: Will use the following as an example. I was pulling a path error on this line
<?php include 'sub_page/about_us/includes/meta.php'; ?>
I was able to use the following instead
<?php include __DIR__ . "/includes/meta.php"; ?>
to fix it. However if i tried to add or take away a directory level from the path it would again return an error. The same thing will not work for any of the other lines/paths.
before
<?php include 'includes/navigation.php'; ?>
after
<?php include __DIR__ . "/navigation.php"; ?>
Any idea?
Set an include path for PHP to "look" for these files: http://php.net/manual/en/function.set-include-path.php or use an absolute path.
If I understand you correctly you want to include a file (services.php) in other files which are on different levels. The included file itself includes some other files.
You must use absolute paths in services.php, e.g.
include __DIR__ . "/navigation.php";
Then it is irrelevant from which directory level you include services.php. __DIR__ will always have the correct path of services.php, no matter where services.php is included.
EDIT: Maybe even clearer...
Assuming directory tree:
<htdocs>/index.php
<htdocs>/includes/meta.php
<htdocs>/includes/navigation.php
<htdocs>/sub_page/about.php
navigation.php is to be included by meta.php.
meta.php is to be included by index.php and about.php.
Paths for includes:
meta.php: include __DIR__ . "/navigation.php";
index.php: include "includes/meta.php";
about.php: include "../includes/meta.php";
Any other file can now include meta.php with a path depending on its directory level.
The __DIR__ in meta.php will always provide the correct path to navigation.php.

php file path require syntax for importing a page

I have a directory called images and about and 3 php file on home directory
and 3 files and have different content
footer.php
This is footer <img src="images/logo.png">
header.php
<h1>Welcome to MyWebsite</h1>
index.php
<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>
Now i have file in directory about as about.php
and it has some contents and below code is
about.php
<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>
And when i open the page about.php, the footer is working fine but the image is not showing up, and image directory has image as logo.png
Even i used realpath to work out with relative paths, but could not display.
Even i tried this one too in footer.php
<?php
define('__ROOT__', images(__FILE__));
?>
This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png); ?>">
I tried out the possibilites of relatives paths, do we have any other thing.
If I'm understanding your question correctly, the easiest solution is probably just to use an absolute path for your image tag in footer.php:
So if images is in your docroot, it would look like this:
This is footer <img src="/images/logo.png">
Including images in the php file makes absolutely no sense.
You have to understand the difference between PHP code and HTML code.
PHP code being executed on the server and have result of HTML code sent to the browser.
Where browser reads that HTML and do additional requests to the server to get images.
Thus, server filesystem root has absolutely nothing to do with browser.
As for the problem - Trott's answer is perfect.
Well if you are using include() to get certain section of the Page with PHP, there will definitely be a problem with links.
I believe the best practice is to call images using CSS. Just get a div with a class or id and place the relative link in the CSS.
It worked for me.
I got the answer for this questions
copy the below code in header.php and it will work every where
<?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?>
And where ever there is a image you src="" just put the below code only with imagename changed, and whatever you want.
<img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">

How to display same header and footer on different pages?

I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>
Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -
Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1
Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1
This makes testing very tedious as I have to upload to the server for every minor change.
Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.
What is the correct way for including header and footer files?
the correct way to include any file is the include() or require() function. Wordpress uses a get_header() function because the header is more then just 1 file, so they created a function for outputting it.
The problem you have seems to be a problem with the $_SERVER variable. It has been quite a long time since I've worked with PHP, but what I would advise you to do is just use relative paths. For example, if the header.php and footer.php files are in the same directory as your index.php, you can just do:
<?php include("header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include('footer.php') ?>
Simple and useful way (I use this in my all projects):
// find Base path
define( 'BASE_PATH', dirname(__FILE__) );
// include files
include BASE_PATH . '/header.php';
include BASE_PATH . '/footer.php';
It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.
edit:
<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
//On Production
$rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
//On Dev server
$rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}
<?php include($rootDir.'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($rootDir.'/footer.php') ?>
?>

PHP - facing problems with require_once() in my site

There's a site I'm developing, and I don't really understand the problem that is occurring...
There's a link inside a table in the Home page. When I click on it, It is supposed to provide some GET parameters to the hyperlinked page. The receiving page processes it, updates the database and redirects to the Home Page.
I've included some necessary php files as "require_once()" in the Home page. But I can't do it on the processing page. It gives some warnings. I don't really understand why and I don't know the solution to this problem. Please help!
Code in the Home page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header_main.php"); ?>
<?php
echo "<td><a href='includes/process.php?id=".$arr['id']."'>Process</a></td>";
?>
<?php include("includes/body_footer_main.php"); ?>
<?php require_once("includes/db_connection_close.php"); ?>
Code in the Processing Page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
//Processing codes
<?php require_once("includes/db_connection_close.php"); ?>
The warnings I'm getting are:
Warning: require_once(includes/db_connection_open.php)
[function.require-once]: failed to open stream: No such file or
directory in C:\xampp\htdocs\MySite\includes\process.php on line 1
Fatal error: require_once() [function.require]: Failed opening
required 'includes/db_connection_open.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\MySite\includes\process.php on line 1
It seems like you are currently in the includes directory because your error says 'in C:\xampp\htdocs\MySite\includes\process.php on line 1'.
However you are still trying to require within includes/ so you end up in includes/includes/ where your files aren't at.
If you are having troubles finding the correct path because you have a file that is loaded through inclusion as well as AJAX for example you can use __DIR__ (or dirname(__FILE__) in older PHP installations) to make sure you have the correct path.
So in process.php that would be for example:
require_once __DIR__.'/db_connection_open.php';
Well, the errors seem to indicate that the files don't exist at the location you're trying. By the look of things, the process.php file already resides in the includes directory, so on your require_once() statements on the page, you need to update the links as follows:
<?php require_once("db_connection_open.php"); ?>
<?php require_once("functions.php"); ?>
//Processing codes
<?php require_once("db_connection_close.php"); ?>
Hope that helps!

Categories