php newbie - non-relative paths?

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post May 1st, 2004, 8:36 am

I'm currently using this to include a file
Code: [ Select ]
<?php
    include 'common/layout.php';
?>
  1. <?php
  2.     include 'common/layout.php';
  3. ?>


which works fine, but my css,js and image files are called using the path from the site root ("/common/filename.ext") so that it makes no difference if I move files around. How can I do the same in PHP?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 1st, 2004, 8:36 am

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Joined: Feb 17, 2004
  • Posts: 279
  • Loc: Buenos Aires, Argentina
  • Status: Offline

Post May 1st, 2004, 10:25 am

Simple, by using an absolute path for the include file, like this: /var/www/includes/myfile.php

Note you need to know the base path in the server which is differen from the path you usually can see in the ftp conection, if you have a hosting provider.
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post May 1st, 2004, 2:02 pm

thanks leo. I'm currently developing on my computer at home, and will then be uploading onto a remote server. Am I right in thinking that I will have to alter all of the paths when I upload?

I'm running on windows, remote server on unix....
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Joined: Feb 17, 2004
  • Posts: 279
  • Loc: Buenos Aires, Argentina
  • Status: Offline

Post May 1st, 2004, 3:04 pm

You're right. No matter if the server will have different OS or the same. Usually installation paths change from server to server.

As I run my own web server, I work with the PHP include path. It's a PHP setting which works much like the OS path. PHP looks for include files in the list of directories in that configuration variable.
But, most hosting providers don't allow its users to change that value, then, if your are going to use such provider, I will recommend not use that approach.

Note, you will have to ASK your hosting provider for the full path, as I told you, from FTP you won't see the real path. I mean, the root path via FTP is usually a virtual root path, not real filesystem path.

If I have to recommend you a workarround for this, I think the best thing you can do is just not move PHP between directories. Then use relative paths. This workarround is supposed to make it easy upload during site maintenace. You will be uploading/maintainning you site more often than you will be moving ONE PHP file from one directory to another.
So, try to make easier "every-day" tasks.
  • diverdan
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Apr 18, 2004
  • Posts: 46
  • Loc: Alabama, USA
  • Status: Offline

Post May 1st, 2004, 3:26 pm

you might can use to get the root path to the script
$_SERVER['DOCUMENT_ROOT'];

http://us3.php.net/reserved.variables
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Joined: Feb 17, 2004
  • Posts: 279
  • Loc: Buenos Aires, Argentina
  • Status: Offline

Post May 1st, 2004, 4:22 pm

That's true as long as you want your include files be able to be located from outside the webserver, which isn't allways a good idead.

I usally put them into a separated directory outside the web-server's document root.

/var/www/docs [document root]
/var/www/docs/images
/var/www/docs/...
/var/www/include [include files, unreachable via HTTP]
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Mar 24, 2004
  • Posts: 1855
  • Loc: Uk
  • Status: Offline

Post May 2nd, 2004, 1:13 am

Thanks for that leo - at least I know where I stand :D

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: demonmaestro and 172 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.