Go Back   CodingForums.com > :: Server side development > PHP

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 12-16-2012, 03:43 PM   PM User | #1
apnavarun
New to the CF scene

 
Join Date: Dec 2012
Location: delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
apnavarun is an unknown quantity at this point
is there any way to use php code in .txt file

hi, I use echo file_get_contents(""); to get text output as I find it easy as compare to javascript. However I find difficulty when the file is non .php so, Is there any way to use echo file_get_contents(""); method on .txt file
apnavarun is offline   Reply With Quote
Old 12-16-2012, 03:48 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can use file_get_contents on any type of file to pull the data as a string. If you have allow_url_fopen enabled, this can also be a remote file (but it will return whatever the parsed output is by the server).
Fou-Lu is offline   Reply With Quote
Old 12-16-2012, 06:34 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
If you mean you want to run a .txt file as if it were php code then yes. Read the file into a variable and then run that through the eval() function.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 12-17-2012, 09:45 AM   PM User | #4
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
Im guessing you are checking what output the .txt file provides and not the actual contents:

IE:
echo("1"); // you want to get the 1 and not the full string?

If you do, there is another way that you could possibly use. You could set the .txt files as executable on your server. You can do this by modifying (or creating) a .htaccess file and adding the following:

For example:

Code:
<Files test.txt>
AddType application/x-httpd-php .txt
</Files>
This will make a file called test.txt executable as php.

However, if you are running user supplied code, IE not code you wrote. I strongly recommend NOT following this approach, or indeed even the eval approach as it is a massive security risk.
idalatob is offline   Reply With Quote
Old 12-17-2012, 09:48 AM   PM User | #5
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
You could then get the result of the page like this:

PHP Code:

$result 
file_get_contents("http://mydomain.com/test.txt"); 
idalatob is offline   Reply With Quote
Old 12-17-2012, 12:16 PM   PM User | #6
alemcherry
New Coder

 
Join Date: Apr 2010
Posts: 55
Thanks: 0
Thanked 4 Times in 4 Posts
alemcherry is an unknown quantity at this point
Your question is not very clear. From whatever I understand, idalatob gave you the answer you are looking for.

However, I should warn you, you are not making a very efficient code and probably not following the best practices. There is overhead associated with parsing a PHP file compared to a text. html file etc. When a newbie want to parse .txt as PHP, it is a clear sign of poor logic and poor idea about optimization. If something is ever meant to be parse, it is better off being saved as .php. Get your basics and logic correct, try to find the simplest solution. KISS is a cliched, still very valid philosophy when it comes to programming.
__________________
Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon
alemcherry is offline   Reply With Quote
Reply

Bookmarks

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 09:30 AM.


Advertisement
Log in to turn off these ads.