SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Sep 2000
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I save the output of a .php file into a .txt file?

    Thanks

  2. #2
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is some code that would work:

    -----------------------------
    If (!($file=fopen("text_file.txt", "w+"))) {
    echo("Error opening the text file for writing.");
    }

    If (!($files=fputs($file, "$variable_you_want_saved"))) {
    echo("Error writing to the text file");
    }

    echo("The file has been successfully saved/updated.");
    ----------------------------------

    Make sure you have the write permissions set on the file you are writing to. If you want the variable to be added to the text file, you just use fopen("text_file.txt", "a") . The fopen("text_file.txt", "w+") will delete all the contents of the file and then it will add the stuff you want in it with the fputs() function.

    I think that will work.

    Chris Roane

  3. #3
    SitePoint Member
    Join Date
    Sep 2000
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply. I have another question.
    So how can I automate this task? For example, I want the output of a .php file saved to a .txt file every hour. If it can't be done with php, then how could it be done?

    Thanks

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cron job! use lynx to run the php script from the command line and have it ouput the file to a text file.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Hmmm...I am kind of stumped on this one. I could create code that would do what you are wanting to do everytime you execute the code, but I don't know how you would do that automatically every hour.

    Someone else might know how to do it automatically.

    Sorry I couldn't help with this question.

    Chris Rone

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add the cronjob using crontab -e
    With this command:

    lynx -dump http://domain/path/script.php

    If the script need some passwords (so it won't run like crazy!), add -auth=user: password (no space, since the smile got in!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    SitePoint Member
    Join Date
    Sep 2000
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers. I really appreciate them.
    I'll try and see what happens...

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Alternatively, if you have the CGI version of PHP installed too, you can eliminate lynx from the equation. Just make your cron job run the script as follows:

    php /path/to/myscript.php > file.txt
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Too bad most virtual hosts don't have this!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

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