php - include html pages outside the root folder - php

On my php site I'm trying to include html pages outside the root folder (the html pages change automatically)
if I include an html page I get the 404 error for the linked files and if I include the linked files too they aren't displayed correctly
path/site/root/index.php
...
include("path/html/index1.html");
include("path/html/index1_files/img.png");
...
path/html/index1.html
...
<img src="index1_files/img.png">
...
result:

You need to specify the directory properly. So, in your case, when linking to the image, replace <img src="index1_files/img.png"> with <img src="./index1_files/img.png">. The ./ goes back into the folder the file is saved in, and ../ goes up one level. Your code is looking for a folder called index1_files within the current page.

Related

Trying to Create an Audit Trail For Log in User History [duplicate]

I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。

How do I include other files in PHP?

I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。

Php includes and folders

I’m creating a new site and in order to have the same navigation and footer in each page I created a footer and navigation files .
I put footer.php and navigation.php inside folder /include
this it the structure of my site so far:
old site folder
index.html
other.html
New Site folder
/index.php
/biography.php
/style folder (style.css, responsive.css)
/images folder
/include folder (footer.php , navigation.php)
/media folder (videos.php)
the problems I have are with images included in footer and links within the navigation menu
for instance, on file biography.php I put on the footer area this calls the footer just fine but the images I have in the footer are broken (the images reside in the images folder)
the same for navigation on biography.php, I have but the links in nav menu go back to old site folder, for instance index.php links to oldsite/index.php instead of newsite/index.php
the file videos.php footer and navigation display all ok , images and links
I guess the issue is with the paths, I tried adding / and dots but it doesn’t work
I don’t know what to do.. :(
You can define definite path to your images:
for example:
<img src='<?="$_SERVER['HTTP_HOST']"."/images/footer.png"?>' />
you can include from your /include/footer.php with
dirname(__FILE__).'/images/filetoinclude'
Edit:
If you want to display images that are served by your webserver, you will have to specify either absolute path /images/images.jpg or the fully qualified path including its host as suggested above
Your problem can solve by HTML.
You must use absolute path in image source.
<img src="/images/footer.png" />
When you use slash in first of src, that mean start path from root of site.

URL path with universal header is not working

I have three pages on my localhost 1 is my index page,2 is my universal header page which is under includes folder of and 3 is my html file which is under html folder.The header file is included in both the index file and html file like that...
for index.php-include("includes/header.php");
for html.php-include("../includes/header.php");
and my header has the link of index.php page that is (./index.php)
Now my questions is that when i open my index page and click on link of index page from my header it takes me to same index.php page but when in open html.php page and then click index.php page link from header it does not go to index.php page but it goes to this page-
(localhost/educational%20website/html/index.php) how to solve that.
And i also want to know that write now i am on localhost but when i make my site live is there any need to change the paths because i am making around 150 pages with your technique plaese so please answer me that kind of technique that is used for both localhost and on live
Your are including paths relatively, use a (absolute) base path in your index.php to fix this:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
One way is to define a variable or a constant for the site's url in the header.php file. Then in all your other pages, you could just use this variable/constant when you need to mention the other urls.
Eg(put this as first line in your header.php file):
define('SITE_URL', 'http://localhost/educationalwebsite');
Here, we have defined a constant named SITE_URL. Then in other pages, you are already including this header file. Isn't it? So, this constant will be available in your index.php, html.php and other pages.
And suppose for a link in your html.php file(to point to the index.php), you could use it like this:
Home
If you want to include link to the html.php file residing inside html folder, it would be like:
HTML
By using this way, if you are uploading the whole site to a live server, you only need to change one line, ie. the first line in header.php, where we have defined the SITE_URL constant. Just change it's value to the new URL of the home directory of your website.
Hope this helps

included files php do not appear in a browser

I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.

Categories