#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595

    File_get_contents


    I am having trouble getting a file and I don't know how to go about debugging it. I THINK it is a file path problem but I simply am not seeing it. I have the following file path in docroot:

    Nevada/Virginia City/PA1200037.txt

    I am using the following code to get that file:
    PHP Code:
    $txt=preg_replace("/ /",'%20',substr($file,0,strlen($file)-3."txt");
    echo 
    "$txt<br />";
    $string=file_get_contents($txt);
    echo 
    "$string<br />"
    The first echo is:

    /Nevada/Virginia%20City/PA1200037.txt

    The second echo is nothing. The file definitely has a line of text in it. I beleive the leading '/' is correct since that is indeed the absolute path with respect to docroot. I'm stuck as to what is wrong with the path such that it is not loading the contents and hope some new eyes will see it. TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    file_get_contents works on the files on the filesystem, not your docroot. unless /Nevada is next to /etc and /usr, you need to add something to the beginning of this string.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Thanks for the reply. I thought that too way back at the beginning. Originally I added docroot to the path. The echo was:

    /var/www/html/Nevada/Virginia%20City/tn_PA210037.txt

    That did not work so I guessed that the path needed to be with respect to docroot. Either way I can't get the file to load but doing an 'ls' on that path yields the desired file so the path is correct.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    I could be wrong, but isn't changing a space for '%20' a URL thing, not a file system thing? I'd suggest trying to get the file with the space instead of changing it out like that and see if it works.

    Comments on this post

    • requinix agrees : spot on
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    But the docs say to use urlencode if the path contains special characters like spaces. It doesn't distinguish between a url or regular file path.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    You mean where it says
    If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
    I would expect that URI here means a web location, and not a file location. That's normally how it works. I'd suggest that you try it and see. Remember that file systems work differently to web addresses, that's why I believe that's a big part of the problem.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    The manual is wrong; a file system path is a URI, but you don't urlencode it.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Oreo is right, the manual is wrong. Without encoding, this may work just as it is. Try it. If not, str_replace(" ", "\\ ", $path);
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Got it! Thanks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo