# [web] PHP include_once(...)

## Recommended Posts

Hi,

I have:
"site/image.png"
"site/data/ogl.php"

Inside "site/data/ogl.php" is the line[source lang="php"]<?php include_once("../header.shtml"); ?>[/source]
Inside "site/header.shtml" is the line[source lang="html"]<img src="image.png">[/source]

The problem is that when I view "site/data/ogl.php", I see the text contained in "site/header.shtml", but not the image, "site/image.png".

I assume that this is because the relative path in "site/header.shtml" no longer works, one directory down. However, I don't know what a good way to fix it is. Help?

Thanks,
-G

##### Share on other sites
It's because the page being shown is site/data/ogl.php and the content of site/header.shtml is just being inserted into that page. You can fix it by either using ../image.php as the src of the img tag, assuming header.shtml is only referenced in ogl.php, or use absolute paths, assuming "site" is your webroot folder, "/image.php" (note the leading slash).

##### Share on other sites
[quote]It's because the page being shown is site/data/ogl.php and the content of site/header.shtml is just being inserted into that page.[/quote]Yes, that's what I thought.[quote]You can fix it by either using ../image.php as the src of the img tag, assuming header.shtml is only referenced in ogl.php[/quote]It is referenced in other places.[quote]use absolute paths, assuming "site" is your webroot folder, "/image.php" (note the leading slash).[/quote]I would rather not do this, because it means changing the directory structure is more complicated. I'm also developing the website on a temporary server, and will be transferring it to a different one. Isn't there another way to do this, but using relative paths?
Thanks,

##### Share on other sites
[quote name='Geometrian' timestamp='1313535797' post='4850074']
I would rather not do this, because it means changing the directory structure is more complicated. I'm also developing the website on a temporary server, and will be transferring it to a different one. Isn't there another way to do this, but using relative paths?
Thanks,
[/quote]
I followed everything until I read this, are you sure you understood what cage suggested?

For example, if I was working on my web site [color="#0000FF"]www.domain.com[/color] and I had a labyrinth of folders and includes, and didn't want to worry about my [color="#FF0000"]src[/color] or [color="#FF0000"]href[/color] values being incorrect, I would use aboslute paths based on my home folder. For example:

[code]<img src="/includes/img/icon.png">
[/code]

No matter where this is called, it will always request "[color="#0000FF"]http://www.domain.com/includes/img/icon.png[/color]" no matter where this file is located, nor where the visitor is on my web site.

This is extremely easy to do,
Mark

##### Share on other sites
[quote]are you sure you understood what cage suggested?[/quote]I thought I did, but now I see I did not. I have reread it more carefully, and I think I understand better. I didn't know that about adding the forward slash at the beginning!
[quote]No matter where this is called, it will always request "[color="#0000FF"][url="http://www.domain.com/includes/img/icon.png"]http://www.domain.co...es/img/icon.png[/url][/color]" no matter where this file is located, nor where the visitor is on my web site.[/quote]I like this idea. However, as I said, I am developing this site separately from where it will be hosted (as in, on a completely different domain name). This leads to a situation where I might be developing the site at:
http://www.domain.com/dev/site/rev2/index.php
But, I might want the final site to eventually be at:
http://www.geometrian.com/index.php

Is there a way to make this method or a different method work subject to this constraint?

Thanks!
G

##### Share on other sites
You could always create something in your header file to handle the location of your site directory structure and keep everything consistent, so if you change hosting providers you can change the variables below instead of hard coding everything:

[code]
<?php
$hosted_domain = "http://www.your_domain_name.com";$images_directory = "images";
?>

// included file
<?php
<img src='$hosted_domain/$images_directory/my_image.png'>
?>
[/code]

##### Share on other sites
You could use PHP variables in your code and just modify them later, like S2P suggested.

Or in your html template put a base href value, which you can change later.

[code]
<base href="http://www.w3schools.com/images/">

<body>
<img src="stickman.gif" />
</body>
[/code]

Note that if you start the src, or href, value with a / (forward slash) that it will still reference the domain name only. In this case, leave out the forward slash I mentioned.

If you had another directory called 'gifs' for example the src above would be changed to:

[code]<img src="gifs/stickman.gif" />[/code]

##### Share on other sites
Thanks you two. I think that these are the best solutions I've seen so far :-)

I propose a third solution, which is to use PHP to find the correct directory (it recursively searches up, until a relative path works). It's hardly very elegant, but it works. When I get some time I'll change it to something nicer.

Thanks for the help!
-G

##### Share on other sites
You're right to avoid writing things like domain names in your code since if it changes then your code will break (the same reasoning goes for using the html base tag). Try dynamically creating the path, something like this:
[code]
<?php
// Somewhere that will always be included. ogl.php should work if you don't have an overall header file.
defined('IMAGE_PATH') || define('IMAGE_PATH', $_SERVER['HTTP_HOST'].'/images'); // In header.shtml (rename to header.php) echo '<img src="'.IMAGE_PATH.'/image.png"/>'; [/code] This saves on having to do any recursion (and stops problems like having site/data/image.png) .You may need to rearrange your file structure so all the images are in a central location (although just setting IMAGE_PATH to$_SERVER['HTTP_HOST'] would work in your current layout but you'll lose separation between public and code files).

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11