Jump to content

Correctly linking images from anywhere


Recommended Posts

So say i have some template images in site_root/images. I have a template header page and a template footer page which basically grab images and make them form the template. Whenever i want to make a new page, i write the content, include the template header up top, and include the footer down below. But, this page i just made could be anywhere. So, i want the template header/footer's images to be linked so that no matter where the page is created, it will find the site_root/images folder to grab the images.

 

Kind of confusing. First, am i being clear enough? Second, can anyone help?

Link to comment
Share on other sites

But isnt there something i can do like $_SERVER or something to find the root of the site, and then do /images/img.gif?

 

So like:

 

$root = $_SERVER_ROOT (or whatever the command is)

 

<img src="$root/images/img.gif" />

 

So no matter where this image is being called from, it will find the root, find the images folder, find the image. Would this work? Is there a command for something like this? Is there a better way to do it?

Link to comment
Share on other sites

But isnt there something i can do like $_SERVER or something to find the root of the site, and then do /images/img.gif?

 

That is all you need. A document's root is "./"

It points to the root of the website where the document resides. if you do "./images/img.gif" you are accessing img.gif through an absolute path.

 

If however you want to access it from other web roots (websites), you must include the fully qualified address (URI). "http://www.something.com/images/img.gif". This is also an absolute path.

Link to comment
Share on other sites

I don't think you understood it yet...

 

Let's say your site is http://www.your.com

This website tree is as follows:

 

./
    images
        img.gif
    includes
        incs
            inc.php
        inc2.php
    index.php

 

index.php resides on the root as does the 'images' folder and 'includes' folder. When someone types www.your.com, they will be reading your root, "./" and index.php will be loaded into the browser.

 

Now... inside inc.php you want to access img.gif. You can do this 2 ways:

 

Relative Path

../../images/img.gif

Absolute Path

./images/img.gif

Link to comment
Share on other sites

Ok, but if i were to use that method, then i couldn't have the ../../ or ./ paths in a header or footer file which is called from other files. The reason being that the "other files" will be in different places. Right? I can only use ../../ or ./ if i know exactly where the image is because then i can count back directories and add the right amount of ../'s. If i don't know where the file is (like if its everywhere since i am including the header and footer everywhere) then i can't use that way.

 

These are my thoughts, i'm no expert. What am i missing?

Link to comment
Share on other sites

These are my thoughts, i'm no expert. What am i missing?

 

Read carefully that link I provided in the beginning.

 

An absolute path maps the file to its absolute position in the directory hierarchy. That is the file position. Period. You can use it from anywhere. It never changes.

 

A relative path maps the file to its position in relation to the document at hand. It changes depending where the document is located.

 

So, because you want to link to the file from a document that is going to be included on different places in your directory hierarchy, you use the absolute path.

 

Just do it.

Link to comment
Share on other sites

I'm going to kill myself, this is ridiculous.

 

Ok...let me explain exactly what i'm doing.

 

here are the files:

www.site.com/index.php (include 'template/header.php')

www.site.com/test/test.php (include '../template/header.php')

www.site.com/tempate/header.php (<img src="./images/img.gif" />)

www.site.com/images/img.gif

 

Ok, so i explained, very simply, what is going on in each file. Why doesn't the image show up in both files (index.php and test.php) where header.php is being included? I think its because the directory is wrong. Did i use the ./ wrong in the img src?

 

I am using this example to demonstrate how the files will be all over calling the same header file which needs to be able to link to the image.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.