View Full Version : fopen php with RELATIVE path

11-02-2006, 06:53 AM
so i'm using fopen to read some xml generated by a php.

$fp = fopen("http://www.site.com/path/xml_generator.php","r");
i really dislike absolute paths... so i tried:

$fp = fopen("xml_generator.php","r");
the problem with that is that is that it's reading the actual php, not what the php generates.

my idea for a solution to this was to use the $_SESSION variables to construct the path of the url, then i can use ../ and such as needed. Something like "$path_to_dir/xml_generator.php" However, this method is not very graceful, as I would have to regex some of the path.

does anyone have any other ideas? or is there an easier way to dynamically get the path of the file? (http://www.site.com/path/)

11-03-2006, 04:16 AM
The reason the first version works is that a http request is made back to the web server and the file is parsed by PHP, so you get the output from any echo/print/print_r statements.

The second version opens and reads the file through the file system and no PHP parsing takes place.

Using output buffering, you can get the output from the second version in a couple of ways, the most straight forward way would be to include the file. See example 16-10 at this link - http://us3.php.net/manual/en/function.include.php

11-03-2006, 06:12 AM
yeah, i've tried that. the problem with that is that it returns a string, and fopen can't read it.

11-03-2006, 06:25 AM
This would replace the fopen code and the code following fopen that reads the contents into a variable/string.

If you post your code someone can give a more specific solution.