Results 1 to 7 of 7

Thread: Change contents of a .txt file with php Code Help

  1. #1
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change contents of a .txt file with php Code Help

    I'm new'ish to PHP so please bare with me. I'll try explain what I'm wanting to do.

    I'm wanting to write a php script that I can run as a cron job on a weekly basis to re-write the contents of a text file.

    Basically, There is a jpg picture on my website that displays depending on whats in the .txt file. This is good as people who admin the site can change this when need be but now I want to be able to change whats in the txt file every friday.
    My script needs to go along the lines of.

    Open txtfile
    delete all text in the file
    write "example text"
    save file
    close

    Could anybody help or point me in the right direction. As I said I'm new to php any help would be appreicated.

    Thanks
    Biggus

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Search the PHP forums here. This question has come up before and I'm pretty sure solutions/resources were offered.

  3. #3
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I found this

    I found this but I can't seem to customize it for my own use.. I'm trying to make sense of it but I'm still new to all this/

    http://www.dynamicdrive.com/forums/a...hp/t-4539.html

    Biggus

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP is unnecessary and inelegant here. You can do it directly in cron: have a line in your crontab like:
    Code:
    0 0 0 0 fri echo -n "example text" > /foo/bar/baz.txt
    Know the right tool for the job. Hint: that's very rarely PHP.
    Last edited by Twey; 10-02-2008 at 02:20 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I've manged it by .php

    //the file you want to open
    $myFile = "yourfile.txt";

    //opening the file
    $fh = fopen($myFile, 'w') or die("can't open file");

    //data you want to write
    $data = "example text";

    //writing the data
    fwrite($fh, $data);

    //close the file
    fclose($fh);

    I've never written a cron before. Would it be better to use that?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Much better, yes. There's no need for an external file, and no need for those five lines of code (three discounting redundant variables).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Trey for the cron job technique I'm using C-Panel and manged to write it and the bonus is it works

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
  •