View Full Version : [Q] Path problem in PHP when using include()...
CyanBlue
11-26-2005, 10:01 PM
Howdy... :)
I have this test file...
http://localhost/dev/PHP/includeTest/includeTest.php
which includes this file...
http://localhost/dev/PHP/includeTest/Directory1/Directory2/test.php
<?php
include("Directory1/Directory2/test.php");
?>
<img src="Flash.jpg" />
When I view test.php alone, it reads Flash.jpg file just fine, but if I view includeTest.php, I do not get that image file... Obviously the test.phpis looking for the Flash.jpg from this location http://localhost/dev/PHP/includeTest/Flash.jpg instead of http://localhost/dev/PHP/includeTest/Directory1/Directory2/Flash.jpg since the base directory has changed...
So, what is the best way to get away with this problem??? I sure don't want to move all the JPEG files into the root directory, http://localhost/dev/PHP/includeTest in this case, and I am trying to figure out to see if there is any way solve this problem...
Flash Gordon
11-26-2005, 11:28 PM
Hey CyanBlue,
How about doing something like the below. Note my code is very beta, I need to tweek the $_SERVER['PHP_SELF']
<?php
$path = "folder/";
if($_SERVER['PHP_SELF'] != $path) {
echo '<img src="' . $path . 'sign up.jpg">';
} else {
echo '<img src="sign up.jpg">';
}
?>
CyanBlue
11-26-2005, 11:35 PM
The problem with that code would be that the test.php file should contain the actual path information and that's the last thing I'd want to do that because that's pretty much the same as hard coding the path in it... :(
Flash Gordon
11-26-2005, 11:42 PM
Here is the only other thing I can think of, but unfornately it is along the same lines.
echo '<img src="' . $path . 'Flash.jpg">';
$path = "foloder/";
include("folder1/temp_testing.php");
If that doesn't work for you, I'll shut up and let Xeef handle it. :)
Notice if you view the first one by itself, it still has its own path because $path is undefined until it gets to the second page.
See ya.
FG
EDIT: perhaps, fopen/fpassthru/readfile would keep the orginal path. Just a thought.
CyanBlue
11-26-2005, 11:55 PM
So, in a sense use that $path variable as if it is a global variable??? I think that's got to work but there's got to be easier way to do that...
Hm... PHP looks so strange to me after so many absent years>>> :D
Flash Gordon
11-27-2005, 01:43 AM
Hope this helps a little: http://us2.php.net/manual/en/function.include.php
If you are seeing this page, you are probably thinking of adding a path, in which case you should look at my add_include_path() on the set_include_path() page. I worked hard to make it a very clear example, so use it!
This is yet another way to include files relative to the current file. I find it easier if you have a lot of includes.
<?php
$prewd = getcwd(); // get the current working directory
chdir(realpath(dirname(__FILE__))); // change working directory to the location of this file
include('includedfile.php'); // include relative to this file
chdir($prewd); // change back to previous working dir
?>
CyanBlue
11-27-2005, 02:18 AM
Tried it and it didn't work... :(
The global variable sorta got me going abit and this is what I have right now...
http://tutorials.flashvacuum.com/index.php?show=Gallery101
The SWF file loads fine because I am pretty much providing the full path to it, but any of the image files does not load because Flash is looking for the images from the document root directory... :(
include works like :
echo "Start";
include "Mindle.php";
echo "End";
//
//
//Mindle.php
echo "Midle";
is the same as :
echo "Strat";
echo "Midle";
echo "End";
expect it's MUCH slower !!! i had a Master file wich was including 5-6 othe files after i have move all to one file it was 3-4 times faster (if not more)
i don't think that there is a better way then FG sad
the path to the included PHP you so or so need to hard code
so why not like this :
<?php
$Path="Directory1/Directory2";
include($Path."/test.php");
?>
alternative you can think on this (but i think in this case it wil not work out)
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using an URL (via HTTP or other supported wrapper - see Appendix I for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using an URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Warning
Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled.
CyanBlue
11-27-2005, 03:12 AM
Yeah... I think what I have is pretty much closest place I can go... and I'll just modify the Flash movies so that I can provide the base path via FlashVars or something for the demonstration purpose... Oh, well... :(
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.