link to file in sub folder - php

please,I am trying to create link to a php file in sub folder using tag but when I am testing the link it gives "NOT FOUND" error this is the code About

Make sure that name of file or folder is correct with caps lock text. something like this in below:
About
If about.php is in pages folder and you are gonna link to another file in this folder, you must remove ../ from the code, something like this:
About
Please provide link of About.php that we'll check it out, it's easy way to find the problem.

Browse to the file in the browser manually, then work backwards.
Draw a map of your directories, for visual aid
For example;
.public_html/
..pages/
page1.html
..files/
...config/
..folder1
..folder2/
index.html
The link from index.html to pages/page1.html would be
Page1
The link from pages/page1.html to index.html would be
Index

Related

How to access same file in different location

Let me explain my need with simple example.
I have folder called "Website" Inside that some sub folders and files.
Website(Main-Folder)
pages(sub-folder)
(files)
page1.php
page2.php
target_page(sub-folder)
(file)
target.php
menu(sub-folder)
(file)
menu.php
index.php (this file is in main folder)
This is my sample folder view, I just included menu.php(navigation menu with links)file in my both pages and index.php file.
In menu.php i have a nav link to target.php page
So, to
access target.php file in
index page mean i have to use Target
Target
access target.php file inside
the pages folder mean i have to use
Target
Is there any short way to access that target page, because I don't like to create multiple menu.php file and set the different location for same file.
Sorry for bad English, Thanks.
[UPDATED]
The best alternative is to define your root path once and for all !
We use another file that we could call whenever we need the PATH. Like for example in settings.php that we save near index.php:
define('My_ROOT' , 'http://'.$_SERVER['HTTP_HOST'].'/');
Then whenever needed, we require that file and so we could write links like follwing :
Target
P.S: Please notice that in my first Answer I used the file path and that could be used for relative inclusion not Frontend links !
Just put your href starting with "/" :
link

How to link to a php file that is in a folder named php

I have made my first website and I have a simple contact form that runs a php scripts to send the details inputted in the form to my email. I initially had all the files in the root directory but I am trying to tidy it up so I now have,
all html files in the root directory, I then have folders for php, css, assets, js.
I know that to link to these I need to just do something like,
<link rel="stylesheet" href="css/contactus.css"/>
I believe this tells the html to look in the root directory, then in css folder and for the file contactus.css.
This seems to work for all the css files but not for the php file ( I get a 404 error). I know this file is correct as when all the files where in the root folder it worked fine.
I currently have,
<form action="php/contactform.php" method="post">
I believe my issue is that when using href it will automatically start in the root folder but as I am not using href in this case it is not starting in the root folder. So I just need to find out what I use to link to a different folder to that of the html file (If I put the php file back in the root folder it works fine.
DIR structure
The action and href attributes work exactly same. The final url depends on how you've written the url in these parameters and what is your current url.
Let's say the current url is http://localhost/my-project/html/my-form.html
If your url in action looks like php/contactform.php this is called path relative url. To get final url the browser will append relative url after last / in current url resulting in http://localhost/my-project/html/php/contactform.php.
You can use .. in the relative url if you want to reference directory higher in the structure. Url ../php/contactform.php will result in http://localhost/my-project/php/contactform.php
If your url in action looks like this /php/contactform.php it is root relative url. The main difference is the starting /. In this case the browser will put your url right after the domain name resulting in http://localhost/php/conactform.php.
Based on your screenshot both /php/contactform.php and php/contactform.php should work fine because the url of the html file should be http://your-domain.example/DJ_contactme.html so both urls should result in http://your-domain.example/php/contactform.php
If you are using mod_userdir your url probably looks like http://your-domain.example/~dannyjeb/DJ_contactme.html in that case the php/contactform.php should work as it would result in http://your-domain.example/~dannyjeb/php/contactform.php
From DJ_Contactme.html you target css file like this:
<link rel="stylesheet" href="./css/contactus.css"/>

how to link html folder's file with php folder's file

I have 2 folders html and php, in html all html files exist and in php all php file exist.
In html folder-> buycars.html exist which link with viewh.php(this is in php folder) how to create a linkā€¦ I use
<a href="php/viewh.php">
that is not working.
you need to jump one directory back
<a href="../php/viewh.php">
You need to go up one directory. To do this you simply append ../ to your directory path;
Click
You can also go up multiple by stacking them like so;
Click
EDIT: #Sourabh beat me to it.

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

header.php include only working on home (index.html) file

I am trying to add an include of "header.php" to my inside pages BUT my PHP syntax is showing up as a comment in chrome's editor. The SAME "header.php" works fine on my home/ "index.html" page but will not on any other. Some points to note:
The route of "index.html" and "inside-page-example.html" are the same, so it's not a route problem.
The code is not written as a comment in the file. It reads:
<?php include ('inc/header.php'); ?>
So frustrating! :(
instead of
<!--?php ... ?-->
Try:
<?php ... ?>
and also change your file extension to .php (everywhere you are using php)
You do need to take out the comments, so that it's just <?php and ?>. The green text in Chrome indicates that it is being read as a comment.
You'll also need to save the file as .html with filetype as "Hypertext Markup Language" not just .txt, but I have a feeling you have that covered.
Also, you'll need to make sure that every file is in the same folder. If "inside-page-example.php" is inside a different folder, then you will need to add a forward slash or /../ for every folder you want to go up. So if you have header.php and index.php and a folder called Resume all inside "Home" folder, then when you are on Resume/index.php, you'll need to say include_once("/header.php");. A really great tool that I use when using includes is to replace the slashes with $_SERVER[DOCUMENT_ROOT]. So the include would read like this: include_once("$_SERVER[DOCUMENT_ROOT]/header.php");. This will grab header.php from the folder at the root of the server. Does that make more sense?

Categories