Jump to content

Include files problem


chiefrokka

Recommended Posts

this isn't making sense for me.  I organized my files into folders such as "admin" and "includes" but I'm now having relative path issues. 

 

Here's how I'm Including the file

<?php include('includes/Functions.php'); ?>

Here's part of a function I'm having trouble with. 

<?php echo '<img src="images/patriots.gif" width="35" height="28">';	 ?>

This Functions.php is in my "/includes" folder so technically when the function is called it should say src="../images/patriots.gif"

 

the problem is from my index.php (in root), when I include my /Functions.php file and call the function, it displays the images fine.  It Shouldn't display correctly because the function should be ../images/patriots.gif but it seems like it's relative to my index.php which is on the root.  so it's just calling the images/patriots.gif

 

When I'm in my Admin page (/admin) and do same thing by adding ../includes/Functions.php and then call same function... the images don't show up because now it's relative to the /admin.php file that included the Functions, so it's trying to display admin/images/patriots.gif which is what the code says.

 

that make sense?  I should simply put ../images/patriots.gif in this function and be done with this... no matter what page your in whether from the root or from /admin folder, the function should still run relative to where the Functions.php file is located.

 

 

 

 

Link to comment
Share on other sites

Seems you are wanting your images to loaded from the root of your website. In that case start the path for you images with a /, eg

<?php echo '<img src="/images/patriots.gif" width="35" height="28">';    ?>

 

This will always load the image patriots.gif from http://yoursite.com/images regardless of your url.

 

nice, you are the man. That worked. I actually just put "/MyGame/images/patriots.gif" and that worked. thank you!

 

but why isn't it working when i just put ../images in my /Functions.php file? why isn't it relative to that Function file instead of the file that's including the Function.php?

Link to comment
Share on other sites

Hi,

 

Hi - I've done a little more lengthy explanation... I wrote it before I saw the other posts and I might as well post it anyway.

 

What I understand is that you include a file in a different directory and then it outputs a link to an image that is correct from its location, but this causes problems because it gets it relative to the index.php file location.

 

If this is correct it is because you are confusing the way that images and PHP includes work.

 

PHP is Server-side, which means that it runs everything and then outputs it in the one file. You could include 50 different files all with <?php echo "cheese<br />"; ?> in them and the server would read this and then send them to the browser as one file with cheese<br /> written 50 times. The browser has no knowledge of how the PHP ran and where the files were, it only knows what file it requested. It takes everything sent back as the one file that it requested.

 

With images, this is a link sent in the HTML and they should always be written so that they work with the file being requested. So if you include 50 PHP files from 50 different directories with a link to image.gif, the browser would find that image.gif in the file location it is currently in.

 

So to solve your problem, every image link in the included file (Functions.php) should be written from the perspective of the index.php file.

 

E.g.

 

Browser Location: www.example.com/index.php

 

Included File: example.com/includes/Functions.php

 

Part of Functions.php:

 

<?php echo '<img src="./images/example.gif" />';    ?>

 

The browser will look for this image in the following location: www.example.com/images/example.gif

 

Link to comment
Share on other sites

You're including a function into admin so the function is executed there.

  So only way around it is to do the / in front of images... or duplicate my Functions file and put one in Admin folder and one in Root and change it accordingly?  seems like there should be a better way as this can cause a mess if it were more than just my echo
Link to comment
Share on other sites

Take a quick look at my above post - I think it clarifies that ...

  gotcha... i posted it right b4 yours came in.  thanks for the explanations guys.  at least I got this stupid little problem solved.  It's fine when I know where the script is being installed such as www.mydomain.com/games/patriots... but if I gave script to a friend who wanted to install it on www.mydomain.com/test/games/patriots the image paths wouldn't work because putting / means root of your domain not root of where the script is installed.
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.