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 6 of 6
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Question .htaccess blocking PHP file_get_contents

    My .htaccess:
    <Files .txt>
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    </Files>

    However, my PHP then can't get the contents of myfile.txt. How can I fix this? Thanks!

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    If you are using Windows and a server like Wamp or Xampp, there are also some bugs that are in combination of Windows, file_get_contents() and localhost that are not going to be fixed. Use cURL instead. file_get_contents has some php.ini values set off by default.

    https://bugs.php.net/bug.php?id=38826
    https://bugs.php.net/bug.php?id=40881

    Use cURL. Off course you have to enable it if its not enabled but it most probably is.
    PHP Code:
    $file_path_str "http://localhost/pathtofile";

    $ch curl_init();

    curl_setopt($chCURLOPT_URL''.$file_path_str.'');
    curl_setopt($chCURLOPT_HTTPGET1);
    curl_setopt ($chCURLOPT_HEADER0);
    curl_setopt ($chCURLOPT_USERAGENTsprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
    curl_setopt($chCURLOPT_MAXREDIRS10);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    $curl_response_res curl_exec ($ch);
    curl_close ($ch); 
    Also remember, Apache in Windows by default can't access files outside www or htdocs folder.

  • #3
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Thank you, but Apache is running on Linux here, not Windows. When the .htaccess file in my first post is not present, I have no problem accessing the files with file_get_contents(). However, for security reasons, I can't have the txt files available through the web.

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    Well, then you need to rewrite the htaccess file and give it access to that folder.

    Code:
    RewriteCond %{REQUEST_URI} !/folderto.txtfile
    But read up on editing it. You don't want to brick the set permissions.

  • #5
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Rather than monkeying around with the .htaccess, would it be possible to simply store the *.txt files above the webroot somewhere? It would be much simpler and if you REALLY don't want to allow access to the text files this is where they probably ought to be anyway.

    That said, if you need to keep things the way they are then (depending on your application) you may need to use your host server's actual IP address rather than 127.0.0.1.

    For example, I believe if you're using file_get_contents with a full URL to your text files (as opposed to using the local filepath) you are making an HTTP request and the IP address in use will be the proper public IP address of the server instead of 127.0.0.1.

    If that doesn't apply to you, then I'm not sure why this isn't working already.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Mooseman (01-12-2013)

  • #6
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Just placed it above the web root. Worked with no problems at all! Thank you!


  •  

    Tags for this Thread

    Posting Permissions

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