php newbie - non-relative paths?

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

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
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

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
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

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
  • diverdan
  • Posts: 46
  • Loc: Alabama, USA

Post 3+ Months Ago

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
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

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
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

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: wpas and 103 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.