Jump to content


php includes: problems with root and folders


9 replies to this topic

#1 PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,055 posts
  • Gender:Female

Posted 31 August 2006 - 06:03 AM

I want to use php includes in one of my new sites. I've never used them before and I'm having a little problems there.
I have my header.php and footer.php and they works fine. I create a new page, called page1.php for example, and I include header.php and footer.php and it works perfectly.
The problem is when I try to create page2.php inside a folder, for example: folder/page2.php.

<?PHP
 include("../header.php");
?>
	  Page2 content goes here  

		<?PHP
 include("../footer.php");
?>

All the images where the includes are don't show up, and the css file I have in header.php neither.
I'm asuming this is a problem with what it considers the root folder, and I've already searched about it in google, but the results didn't help me to find out what is the problem and what I have to do to solve it.

#2 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 31 August 2006 - 06:36 AM

The way I do this:

In my index file I have the following line:

define( 'PATH', '/path/to/files/' );

And I use it to include other files like this:

require_once( PATH . 'my_file.php' );


#3 PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,055 posts
  • Gender:Female

Posted 31 August 2006 - 07:28 AM

Ok, but what i have to write in the source code of /folder/page2.php to include a php (header.php) that calls to images that arent at /folder/ ?. I mean, in header i call to the images this way -> "folder_to_images/hello.jpg".
But when i include the header.php in page2.php, obviuosly there isnt "folder_to_images/hello.jpg" because it is in other level.

My structure:

/index.php
/header.php
/footer.php
/folder_to_images/hello.jpg
/folder/page2.php

Thanks Ruben K for your help :D

#4 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 31 August 2006 - 07:36 AM

you could just use link images directly ( <img src="http://psgirlssite.com/image.gif" .... /> )

#5 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 31 August 2006 - 07:37 AM

Are you trying to include files or display images in relative directories?

#6 PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,055 posts
  • Gender:Female

Posted 31 August 2006 - 07:55 AM

Thanks coolaid, I think I'll do that if I don't find any other solution.

Ruben K, I guess so, is it wrong?

I put an include to "../header.php" in /folder/page2.php, so if that's a relative path, yes.
header.php have images inside, for example "images/hello.jpg"....so if i use include header.php inside /folder/... images don't show because there is no /folder/images/hello.jpg
Is a bit difficult to explain :P

#7 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 31 August 2006 - 08:40 AM

Well if you would like to use relative URLs for displaying images, I recommend you use this:

<base href="http://yoursite.com/site-dir/" />

This means you can link to

images/logo.jpg

and the browser will read

http://yoursite.com/...images/logo.jpg

even if you're not in site-dir/ and use relative path

#8 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 31 August 2006 - 09:10 AM

include utilizes the unix file structure as opposed to the web server file structure.

so, let's say the unix path to your web site is /home/psgirl/public_html, when we do
include "../header.php"
it's looking for header.php in /home/psgirl (assuming we have the file with that include in a php file in public_html)

i do like what Ruben K does.

define( 'PATH', '/path/to/files/' );

require_once( PATH . 'my_file.php' );

Edited by SpatialVisionary, 31 August 2006 - 09:11 AM.


#9 PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,055 posts
  • Gender:Female

Posted 31 August 2006 - 09:30 AM

Thanks a lot guys. I did what Ruben said about the base href and it works now :P
Thanks!

#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 31 August 2006 - 08:02 PM

View PostSpatialVisionary, on Aug 31 2006, 08:10 AM, said:

include utilizes the unix file structure as opposed to the web server file structure.

so, let's say the unix path to your web site is /home/psgirl/public_html, when we do
include "../header.php"
it's looking for header.php in /home/psgirl (assuming we have the file with that include in a php file in public_html)
No.
Includes use what ever file structure is available to them, and relative paths are universal.
So, let's say the unix path to your web site is /home/psgirl/public_html, when we do
include "../header.php"
It's looking for header.php in the folder above what the initial file is in.
i.e.
domain.com/folder/file.php is in: /home/psgirl/public_html/folder/ and makes a call to: ../header.php, then it will look for: /home/psgirl/public_html/header.php

Of course, i didn't need to explain that, we already knew that (it's just setting me up).

View PostPSgirl, on Aug 31 2006, 06:28 AM, said:

My structure:

/index.php
/header.php
/footer.php
/folder_to_images/hello.jpg
/folder/page2.php
/home/psgirl/public_html/ lets assume that is your directory structure (as it typically is with most sites).
According to a browser, /index.php is located in that directory. Now, if you were to say: include('/index.php'); from a php file, it would look in: ../home (note the dots and the above explaination).
Now, if you tell your browser to go to domain.com/folder/page2.php, then you have to use relative paths from within your php to access header.php, and relative paths to the images (as the browser would look for domain.com/folder/images/file.gif as opposed to domain.com/images/file.gif).

This is why most people like to use .htaccess and the query string of a url to load different pages. If the browser thinks every page is index.php, then every image/css/js file is relative to index.php, and index.php can include all the different pages you need (of course, that has it's inherit security risk, but that's so well known we need not discuss it).

And the whole point of posting this was so i would never have to again :)

Edited by rc69, 31 August 2006 - 08:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users