[web] PHP include_once(...)

Started by
7 comments, last by cagecrawler 12 years, 7 months ago
Hi,

I have:
"site/image.png"
"site/header.shtml"
"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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
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).
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.
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.
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,

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]


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,

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 and I had a labyrinth of folders and includes, and didn't want to worry about my [color="#FF0000"]src or [color="#FF0000"]href values being incorrect, I would use aboslute paths based on my home folder. For example:

<img src="/includes/img/icon.png">


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

This is extremely easy to do,
Mark
Mark A. Drake
OnSlaught Games
Mark Drake
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!
No matter where this is called, it will always request "[color="#0000FF"]http://www.domain.co...es/img/icon.png" 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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

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:



<?php
//header file
$hosted_domain = "http://www.your_domain_name.com";
$images_directory = "images";
?>

// included file
<?php
<img src='$hosted_domain/$images_directory/my_image.png'>
?>
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.


<head>
<base href="http://www.w3schools.com/images/">
</head>

<body>
<img src="stickman.gif" />
</body>


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:

<img src="gifs/stickman.gif" />
Mark A. Drake
OnSlaught Games
Mark Drake
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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

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:

<?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"/>';

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).

This topic is closed to new replies.

Advertisement