I'm using php include. Now the files are in the sub-folder.
The error goes exactly like this:
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
The HTML/PHP file is this:
<html>
<head>
<title>PRINT DESIGN</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="../images/art_favicon.png" type="image/x-icon"/>
<link rel="stylesheet" href="../css/body.css" type="text/css" media="screen"/>
</head>
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<body>
<div id="contents">
</div>
</body>
<?php include ("footer.php"); ?>
</html>
Assuming you have the paths correct and files in place you can try this...
<?php
include ("sub-folder/headertop.php");
include ("sub-folder/header.php");
include ("sub-folder/nav.php");
?>
The thing you want to avoid is having to change the path to an include on each page. You can do that with something like this:
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"; ?>
That will work nicely online, but to work in XAMPP, you need to set up a vitrual host so that the link points to the same thing: http://sawmac.com/xampp/virtualhosts/
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
says there is NO headertop.php file in your directory
check if the file exists: D:\ROLDANKING\xampp\htdocs\mysite\pages\headertop.php
also you can just use:
<?php
include ("headertop.php");
include ("header.php");
include ("nav.php");
?>
instead of:
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<?php include ("SUB_FOLDER/headertop.php"); ?>
try this
include '../headertop.php';
or
include '/headertop.php';
I have come across dozens of webpages over the last 3 days and I think I have collectively tested exactly what most people coming here are looking for.
ABSOLUTE PATHS. They must be manually established on your local host, and then again on your Live website, but this is nothing shy of declaring a variable set to a particular pre-built function.
Stay with me..
Every time I reference a link in a php (includes and echos), or in html and css, I reference a variable set to the root directory + that original link.
e.g.
background-image: url(<?php echo $root; ?>images/bg-0.jpg);
The only downside to this is the visibility of extra code and tediousness of adding a $variable to each and every link in a css or php document. And lord forbid javascript, because I havent even touched the root of that. Heh. Puns.
Anyways, to make this work..
In my Styles.css Doc, I simply convert it to Styles.php, encase the CSS code in
<style type="text/css"> *CSS* </style>
tags, which enable the echo of a PHP variable but as a string, or in our case an absolute path precursor.
In my header file, I now include my CSS as a PHP include with a new Root Variable.
include $php_root.'includes/css/styles.php';
Notice how this $variable is different than the echo's? The way I declared the variables plays a huge role in how CSS/HTML perceives a root destination, and how PHP sees it.
So these were my set variables.
// WAMP Localhost
$root = "http://localhost/PZD/";
$php_root = $_SERVER['DOCUMENT_ROOT'] . "PZD/";
// Live Server
$root = "http://prozechdesigns.com/";
$php_root = $_SERVER['DOCUMENT_ROOT'];
These may be set in your main header.php include itself.
Or if you're like me with database files to connect to and communicate with, you might pair it with a database connect.php file. I did; that way, when I call upon my init.php file or need to edit the init.php, I do not need to worry about which $root variables are being used after overwrite between localhost and live website.
You may not be using WAMP as you're reading this, and at your time and date or configuration, your PHP Root location may not be set like mine is. But if you are using WAMP and perhaps wish to find out where your root is set or change it, look for the httpd.conf file located by default in "wamp/bin/apache/apache#/conf".
Related
I am refurbishing a website for a friend, e.g. making it more easy to program/maintain.
The server is running PHP 5.6 and in order to make life easier for me I wanted to uses php's include function to easily include stuff like the head or menu in every web page.
The file structure I use is index.php in the / directory and e.g. history.php in /pages. The files I am including e.g head.php lie in /php.
My problem is that in index.php
<?php include ('php/head.php'); ?>
perfectly executes and includes the designated file but in all sub directories such as /pages the same php code in history.php just doesn't execute at all leaving me with a blank line in the source code. I figured that this has to do with my PHP config or that said might be wrong, but I couldn't find the issue. I also tried calling the tech support of my web hosting provider but although they told me that everything should be working now I still get a beautiful blank line.
I've been searching for a solution to my problem for quite some days now, but I sadly haven't had any luck so far.
Hope the community can help
Thanks in advance
If you set the include_path to the full path then no matter in which sub-directory you have scripts they will always use the full path to the required file.
<?php
/* If you set this on every page the path will always be correct */
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/php/' );
include( 'functions.php' );
?>
<!doctype html>
<html>
<head>
<title>Include PHP files</title>
<?php
include( 'head.php' );
?>
<body>
<?php
include( 'menu.php' );
?>
<!--
regualar html,javascript etc
-->
<?php
include( 'footer.php' );
?>
</body>
</html>
Is it simply a file paths issue? If you're in a sub-map, then the path to head.php will be different:
<?php include ('../php/head.php'); ?>
I have researched so much in blogs and forums and can not seem to find the solution to this problem. I am using the Wamp directory: C:\wamp\bin\apache\apache2.4.9\htdocs\lr
the lr at the end of the /htdocs is just the folder that I am holding my webpage in.
The code inside of my index.php :
<html>
<?php include('includes/head.php'); ?>
<body>
<?php include 'includes/header.php'; ?>
<div id="container">
<?php include 'includes/aside.php'; ?>
</div>
<footer>
© phpacademy.org 2011. All rights reserved.
</footer>
</body>
When I load the index.php in chrome or any browser the only thing that I am seeing is the footer which happens to be the only thing that is NOT set up in a php include tag.
Here is the code inside of my head.php
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css">
Very basic code which only gives me more confusion on why it is not working. If you would like more examples of code that I am using let me know. I will gladly provide some as I really need this to continue creating the database for my login/registration page(s).
It is likely that PHP does not find the files to be included.
Are you sure the folder 'includes' is in the same directory as your index.php?
Also, I would suggest to use "**/**includes/..." with a slash at the start to be sure that it searches the included files from the root directory, and not from the relative path of your index.php.
Besides, shouldn't your root be C:\wamp\www\ in WAMP?
You can check if the included file exist, this can give
you a clue to what is going on
if(file_exists('includes/head.php')):
include 'includes/head.php';
else:
echo 'file either not readible or does not exist';
endif;
Try to close head tag first in your head include file, if that doesn't work, I will dig deeper :D
EDIT:
Sometimes there are problems with file path. I usually use:
<?php include $_SERVER['DOCUMENT_ROOT']."/filename.php";?>
In Your case that probably be:
<?php include $_SERVER['DOCUMENT_ROOT']."/lr/includes/header.php";?>
I have looked around at a few different questions about the same sort of problem I'm having. I have took a solution and adapted it to my own project.
Here is my directory structure.
/css
-style.css
/includes
-shop.css
-header.php
-footer.php
/php
/js
/shop
-index.php
-index.php <-- homepage
-config.php
Inside my config.php I have
define('ROOT_PATH',$_SERVER['DOCUMENT_ROOT']);
My header.php
<?php include './config.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo '<link href="'.ROOT_PATH.'/css/style.css" rel="stylesheet">';?>
<?php if ($_SERVER['REQUEST_URI'] == '/shop/'){echo '<link href="'.ROOT_PATH.'/includes/shop.css" rel="stylesheet">';} ?>
</head>
The only problem is, for any other page other than the root index.php file, the path for the config.php file becomes incorrect. Thus the CSS paths then become incorrect as ROOT_PATH isn't defined anywhere.
What would be the best way to handle paths when using includes?
Use $_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_ROOT'] gets the document root for eg var/com/images. $_SERVER['HTTP_HOST'] will get current url like http://example.com/images.TYour code should look like this
define('ROOT_PATH',$_SERVER['HTTP_HOST']);
And include this way
<?php include'../config.php';
Hope this helps you
Maybe using relative paths for the include in the other index.php file.
<?php include '../config.php'; ?>
You are using the server's actual filesystem path to refer to your stylesheets. That's like trying to do something like:
<link href="C:\your_website_path/includes/shop.css"...
and wont work.
I would recommend to change that to something like:
define('ROOT_PATH', 'http://www.your-website-url.com/');
Regards,
what you need to do is make the include for your config absolute, not relative. you begin the path with a dot ./config which means its relative. instead set up your header to include the config file with an absolute path like this
<?php include '/home/user/config.php';?>
This way, any page can find the file no matter its location in the directory structure.
I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.
Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>
Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.
I am able to automatically make a new webpage with user input, but there is only one problem: the css doesn't work on this. I know how to attach a css file to a HTML file, but this time, it just doesn't work. To help you visualize this, here is the code I have so far.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="/stylesheet.css">
</head>
<body>
<--content for webpage here-->
</body>
</html>
So, that is the relevant HTML to go with it, and the connection of the CSS to the HTML was literally copy and pasted from another page that worked. I also made sure not to use divs, but to use classes because it could've caused problems. There is also a problem, that when I try to add the header and footer file to my document using php, there is an error as follows: Warning: include(header.php) [function.include]: failed to open stream: No such file or directory in /home/content/55/10690555/html/words/tower.php on line 2
I have no idea as to why this is happening on this page but not any other ones, so any help would be greatly appreciated. By the way, the file with the errors, is the template for creating a new document using PHP if that helps at all.
If you are using php to include the CSS files, you might probably be using include(header.php). This might not work sometimes. Use the absolute path instead.
To get the root location, include $root = realpath($_SERVER['DOCUMENT_ROOT']); at the top and append$root variable before the filename inside the include() function.
You need to properly specify the header.php file in relevance to your webpage file. So depending on your folder structure the correct line of code will vary.
ie. if this is your folder structure:
/folderone/header.php
/foldertwo/webpage.php
This is what you need to use: include('/folderone/header.php');. If header.php is in your root folder you only need to use include('/header.php');
The point is, include('header.php') tells php to look for the file header.php on the same folder as the webpage.
It is a clear indication of file path problem. Please check the new which you are creating by script have in the same path or directory of other files. I also observe that in your css file inclusion you are using '/' this mean respect to the root of the server. So, be sure your css file is on the document root otherwise use relative url.