If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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

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).

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.

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.