Page 1 of 1

including with relinking

Posted: Mon Apr 28, 2008 9:00 pm
by kendall
hey guys,

got a question...how do you guys handle the url paths of links and images for included files when the 'includer file' is different from the 'included file' ...

to use a pseudo example
i have /default/test1.php
and i have /custom/test2.php

in test1.php i include test2.php with test2.php having a link to an image (image/test.jpg) /custom/images/test.jpg. The obvious solution would be to 'copy' the test.jpg over to the default/images/ but is there anyway to avoid having the same file in to different places?

Re: including with relinking

Posted: Mon Apr 28, 2008 9:23 pm
by John Cartwright
use absolute paths

Re: including with relinking

Posted: Tue Apr 29, 2008 11:54 am
by RobertGonzalez
Or set a global var that points up to the root of your app and use that throughout. Though I like Jcarts idea better.

Re: including with relinking

Posted: Thu May 01, 2008 11:20 pm
by Ambush Commander
The usual solution is absolute paths. If you want to get fancy, you can try to run the included file through a post-processor that munges things accordingly.

Re: including with relinking

Posted: Wed Sep 17, 2008 2:17 am
by Popcorn
sorry, absolute, that would mean a 'literal' right?
so it is common to use a literal over a 'global' var?

Re: including with relinking

Posted: Wed Sep 17, 2008 6:10 am
by kendall
Yeah....i had to use absolute....theres no getting around it...

tanks guys

Re: including with relinking

Posted: Wed Sep 17, 2008 5:49 pm
by josh
In the including file:

Code: Select all

 
// this file is located in /var/www/public_html/tests/
define( 'MY_PATH', dirname(__FILE__));
include('../other/arbitrary/path/included.php');
 
in included.php:

Code: Select all

 
echo MY_PATH; //  /var/www/public_html/tests/
 

Re: including with relinking

Posted: Fri Sep 19, 2008 8:25 am
by Popcorn
i don't know why anyone would want to use path literals, ever. when adding to sites or doing recovery or upgrade installs (yuck), path fixing is pretty much step number one.
'jshpro2's define() is good but i'd say you'd want this to be available everywhere and not have only *some* 'big' concept values like the example 'MY_PATH', available just for the instance you are fixing.
there are lots of $_SERVER variables for filenames and paths that are i think used by loads of us. why not, like 'Everah' said, define a few more constants (or even variables) that refer to the documentroot and also some other common dirs.
they can be available everywhere like $_SERVER and mean you never have to use literals.
despite fear about global vars, and modularizing scripts that depend on particular vars, surely adding and maintaining those vars has to be easier than littering your scripts with literals ...

Re: including with relinking

Posted: Fri Sep 19, 2008 11:35 am
by RobertGonzalez
$_SERVER variables are insecure and should not be used for anything that have any importance to you.

jshspro's suggestion, for the time being, is a good way to go. And a heads up, in PHP 5.3 there is going to be a __DIR constant introduced that will take care of all of this for you.

Re: including with relinking

Posted: Mon Jun 15, 2009 2:46 am
by Popcorn
ok, totally clueless about this stuff, but i looked a little but what i can see is that there is a specific problem with $_SERVER['PHP_SELF'] and a couple of others (http://seancoates.com/xss-woes).

i am using $_SERVER['DOCUMENT_ROOT'] and $_SERVER['SCRIPT_FILENAME']. anyone know of problems with these? (you know there's no mention in the PHP docs of the PHP_SELF thing. they do omit things sometimes don't they?)

AFAIK , it looks like "__DIR__ == dirname(__FILE__)" which is for the current file (not the main/parent/includer), so this may not help.

Re: including with relinking

Posted: Mon Jun 15, 2009 6:36 pm
by RobertGonzalez
Not all server variables that you see in tutorials are going to be available to you. PHP_SELF is one of them. Never rely on it being available to you. Instead consider using either the constant __FILE__ or the SCRIPT_FILENAME server var.

Re: including with relinking

Posted: Tue Jun 16, 2009 6:47 am
by Jenk
.. or

Code: Select all

realpath('.');