FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


help with location





aningbo
to display a picture in the "images" folder located in main base from another folder "news", i use this code:
Code:

<?php include("../head.php"); ?>


while the head.php in the main base contains:
Code:

<img src="images/mypicture.jpg">


i get the output as http://127.0.0.1/news/images/mypicture.jpg

how doi set it right so that it displays the correct url and that is: http://127.0.0.1/images/mypicture.jpg

its killing me, i just cant figure out!!!
SlowWalkere
Well, the problem is that when you use the #include, the text is being written directly into your /news file and being executed there. So the image tag thinks your looking for an images folder that is a subfolder of the one you are currently working in.

One way to fix it is to include the whole URL of the file when you link to it. That way you can access it from any folder. So use
Code:
<img src="http://127.0.0.1/images/mypicture.jpg">


This of course will be a problem when you upload your website to a remote server, or later move servers, because you'll have to go back to edit all of your links again. So, a similar route would be define a variable as $root and set it to be "http://127.0.0.1" Then, use {src="$root/images/mypicture.jpg"} for your tag. When you put the site on line, just go back and change the $root variable definition to your new address.

Alternatively, if that image does not rely on any php to be shown, you can separate the php script from the html in "header.php" and use a template to format your site. But, that relies on you using some kind of editor that supports templates. That way, the links in the header section should be automatically updated for the subfolders.

- Walkere
ammonkc
or you could just keep it relative like this:
Code:
<img src="../images/mypicture.jpg">

Either way works fines. But I tend to like relative linking because it makes your app more portable.
SlowWalkere
Normally, a relative link would be the way to go. But I think in this situation it will eventually break...

The header.php file is in the root folder. The file with the problem is in the news folder. So, in that file in the news folder, the "../" prefix would jump up one directory and allow you to access images from the root directory fine. But if there is another file in the root directory that includes "head.php" it won't work right because it tries to jump up a directory when there's nowhere to go. Likewise, if a file is later placed two directories from the root (news/archives/lastyear.html, for example), the link will point to the /news/image directory instead of the /images directory.

The only way I can see to fix this is to write a script that knows the root address of the page, takes the current URL, then figures out how many subdirectories need to be exited before you get to the root folder and use that to add the right number of "../"s. But, at that point, you might as well just use a variable to hold the root address and prefix all of the links with it.

Also, if you define the variable in an include file (like header.php), the website is still very portable. All you have to do is change one line of code when you transfer the site to a new server.

- Walkere
Agent ME
What I always do is like this-

head.php
Code:
<img src="<?php echo $site_root_path; ?>images/mypicture.jpg">


And any file that includes it would set $site_root_path correctly before including. IE the page lol.com/news/lol.php would be
Code:

<?php

$site_root_path = '../';
include($site_root_path.'head.php');

?>
aningbo
hey people thanx for the replies.. i actually firgure out right after i post this and didn't had the time to reply.

i'm actually working with a skin on a different platform. anyway i got it sorted out and yeah thanx for the replies.
Related topics
Header("Location: PROBLEM");
can you help me?
What do you think about online couse ?
php script in php nuke - need help!
Need help how to move a forum - Offer 100 frih$!!
Php parsing error! Need help
Uploading a .php file! NEED HELP thanks
Need help making logout script
Need a little help
trying to put up forums... help?
Need help with Members area/link adder
Need help with an error
Domain help
Please Help? CGI form mail on my site... only 2 questions!
Need help with subdomains of my hosting account!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.