Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2013, 06:54 PM   PM User | #1
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
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!
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 01-07-2013, 01:34 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 01-07-2013, 03:23 PM   PM User | #3
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
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.
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 01-07-2013, 03:32 PM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 01-08-2013, 01:45 PM   PM User | #5
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Users who have thanked Rowsdower! for this post:
Mooseman (01-12-2013)
Old 01-12-2013, 08:54 PM   PM User | #6
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Just placed it above the web root. Worked with no problems at all! Thank you!
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Reply

Bookmarks

Tags
htaccess, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 PM.


Advertisement
Log in to turn off these ads.