Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Posts
    124
    Thanks
    7
    Thanked 0 Times in 0 Posts

    fopen php with RELATIVE path

    so i'm using fopen to read some xml generated by a php.
    Code:
    $fp = fopen("http://www.site.com/path/xml_generator.php","r");
    i really dislike absolute paths... so i tried:
    Code:
    $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/)

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Posts
    124
    Thanks
    7
    Thanked 0 Times in 0 Posts
    yeah, i've tried that. the problem with that is that it returns a string, and fopen can't read it.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •