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
    New to the CF scene
    Join Date
    Dec 2012
    Location
    delhi
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    You could then get the result of the page like this:

    PHP Code:

    $result 
    file_get_contents("http://mydomain.com/test.txt"); 

  • #6
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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


  •  

    Posting Permissions

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