I tried to use a URL in the parent directory of the root but it's not working in the HTML attribute of the PHP file.
<?php
include ("../func.php"); // working, will include the file in the parent directory
?>
<img src='../testimg.png'/> <!-- It only looks inside the current directory-->
<img src='..../../testimg.png'/> <!-- If I mess around with the relative path, it still looks inside the current directory-->
However, if I delete the PHP code and change the file extension to .html, the relative path in img src works normally.
Why is this happening and how could I solve the problem? I am wondering is there any PHP configuration file I need to adjust?
I have been stuck with this problem for a long time and googled a lot but didn't find a working solution. I would be appreciated for any help.
I tried your code in my local with the following directory structure
root_folder
|---my_folder
|__index.php -> (this file contained your code above)
|---func.php
|---testimg.png
<img src='../testimg.png'/> showed my image perfectly.
Related
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"/>
I have an issue with finding paths with includes in my site. I use the method i know but coud not get it right: Here is how my site is composed
1- Main foldername is:kbaboutik: Inside we have:
images
css
includes
index.php
subfoldername:Man-clothes
index.php
So, the issue have is that I have tried to include header.html.php from the includes folder into man-clothes index.php with this method:
<?php
$pathroute = $_SERVER['DOCUMENT_ROOT'];
include_once $pathroute."kbaboutik/includes/header.html.php";
?>
But it does not work the way expected; the html element of the header.html.php apppears unstyled and the images also do not not appear.
Can somebody help me out?
Thanks
Check the path in your header.html.php. Images and stylesheets should look like this:
<img src="/images/model1.jpg">
Starting with a "/" and then a path relative to your document root.
I'm developing a website for my conclusion work at school. I'm using XAMPP v3.2.1. to localhost the site.
My site's folder's are configured just like this in htdocs folder:
ibnm
css
js
img
...
site
about
midia
...
includes.php
index.php (HOME PAGE)
My problem starts here: on index.php I'm including includes.php, that's a simple file with define() functions to the folders of my site so I can print the constant on the HTML tags of the site as URL (just like below)
//includes.php
<?php
define("css", "localhost/ibnm/css");
?>
//index.php
<?php
include_once("includes.php");
?>
<link href="<?= css; ?>/bootstrap.css" rel="stylesheet">
But when I do this, the CSS don't function. When I see an <a> tag with the previously defined URL on page it looks like
localhost/ibnm/site/localhost/ibnm/css
instead of
localhost/ibnm/css
It's confunsing 'cause if the <a> tag doesn't have any value (href="") it output localhost/ibnm/site/.
What can be wrong? XAMPP or coding?
Any url is not starts with http then browser will assume that its relative path so it will append to your current path, thats why your getting localhost/ibnm/site/localhost/ibnm/css.
And one small correction in your code, its not good idea to hard code server name in the code, better to get server name dynamically. So that you no need to change while deploying your site in real server.
//includes.php
<?php
define("css", $host='http://'.$_SERVER['SERVER_NAME'].'/ibnm/css');
?>
This is because the browser think localhost is a folder, and then do this ontop of the current path, to fix is just add http:// before the localhost
define("css", "http://localhost/ibnm/css");
Instead of using absolute path you can just add one slash before CSS path -
<link href="/<?= css; ?>/bootstrap.css" rel="stylesheet">
Before going on, I'd just like to say that I've tried pretty much everything, and I read a lot of questions here at Stackoverflow, but none of the solutions worked.
So here is the deal, I have a file ("empresaConf.php") that contains this code:
<?php
include 'navigator2.php';
?>
<div class="contTudo">
<div id="bgCorpo">
<div class="empresaConf">
<?php
include 'sqEsqInc.php'
?>
</div>
</div>
<?php
include '../rodape.php';
?>
</div>
</body>
</html>
As you can see, it is including "rodape.php". The problem is that in "rodape.php", I have an image that is in another folder, so when I include "rodape.php" in "empresaConf.php", the image path should change, since PHP include consider the file path to be the one that you're including other files, making rodape.php's image broken when I access "empresaConf". I want something to fix that so I have the image no matter what file I'm in. Here is the "rodape.php" code:
<div class="empresa">
/* below in the img src tag you can see what I'm talking about */
<div class="redeSocial">
<a href="http://www.facebook.com.br">
<img src="imgs/icons/iconFace42.png" class="iconeFace">
</a>
</div>
</div> <!-- Fim div .empresa -->
The folder structure is: "rodape.php" is in the root directory and "empresaConf.php" is inside a folder named "Empresa" (I would post a img but I can't).
Thanks in advance.
EDIT:
Sorry, I forgot a crucial detail, I'm also including "rodape.php" in other files such as "index.php". So if I change the img src to correspond "empresaConf.php", then in the "index.php" the image will be broken, since it's in the root directory, same as "rodape.php".
If it's an image accessible from the public side of things, the image can probably be accessed by the directory root. So if your index.php is in /index.php where / is the web document root and your image is in /imgs/icons/iconFace42.png you can access the image by
<img src="/imgs/icons/iconFace42.png" class="iconeFace">
Note the leading slash in the src attribute.
You have some options.
If rodape.php will only be accesed from that folder level, simply change the src to ../imgs/icons/iconFace42.png, if you will be changing folder deep then use an absolute path to the image. Another way would be add php code to set the src but that would be wast of resources.
When you include ../rodape.php, think of it like just putting the code from that file directly in the position where it was included. The path string will not change, as it has not been executed until it has been included.
In short, that is to say, things will be relative to the file that's being executed, not the file(s) that's being included.
<img src="../imgs/icons/iconFace42.png" class="iconeFace"
Using that image code should resolve your issues.
This is all that is contained on my home.php page
<?php
include ('../bgs/bg-verbier.html');
include('../menus/menu-verbier.html');
?>
both of the requested pages lie in the parent directory. The /menus/menu-verbier.html has posed no problem from the include function, however the '../bgs/bg-verbier.html is. The file as been found however the images aren't displaying.
The path of the images is working on a .php page that lies in the same parent directory,
Here's the ../bgs/bg-verbier.html' file:
<div id="bg">
<b><i><i><img src="images/verbier/0.jpg"></i></i></b>
<!--b:holder, i#1: large canvas, i#2: center verticaly -->
<b><i><i><img src="images/verbier/1.jpg"></i></i></b>
<b><i><i><img src="images/verbier/2.jpg"></i></i></b>
<b><i><i><img src="images/verbier/3.jpg"></i></i></b>
</div>
<script type="text/javascript" src="js/scaler.js"></script>
<script type="text/javascript" src="js/example.js"></script>
Your images folder must be in the same directory as the php file that is calling it. Otherwise, you must specify the relative path to the images folder.
When you include a file, it's like if you were to copy & paste that file's contents into the PHP file.
So, it's like the PHP file is asking for images/verbier/0.jpg, which means it's looking in the same folder as the PHP file.
I suggest you edit the HTML file so it uses absolute paths to the images, like:
<img src="/menus/images/verbier/0.jpg">