/    Sign up×
Community /Pin to ProfileBookmark

add to end-clear-get text from a .txt file

i am a newbie in php and i was wondering if there is any way to do the following:ex using a function ex:$us = open(‘haha.txt’)

[list=1]

  • [*]

    add text to the end of a text file


  • [*]

    clear the file


  • [*]

    get the contents of the file and put them in a varable
    [/list=1]
    please help me

  • to post a comment
    PHP

    24 Comments(s)

    Copy linkTweet thisAlerts:
    @JonaJan 14.2005 — [font=trebuchet ms]You can't do it in that order. Do you want a function that opens a file, appends to it, reads it, deletes it, then returns the value before the contents were deleted?[/font]
    Copy linkTweet thisAlerts:
    @emblemauthorJan 15.2005 — i mean

    i would like one function to ad to the end of the file

    another that clears the file

    and yet another that takes the contents of file and stores it in a varable

    [b][COLOR=red]3 diffrent functions[/COLOR] [/b]
    Copy linkTweet thisAlerts:
    @JonaJan 15.2005 — [font=trebuchet ms]Try looking up the filesystem functions at PHP.net. I would just use one function for all of those. Try this function I wrote for my [url=http://www.cmmwebdesign.com/v3/software/chategory]Chategory[/url] software:[/font]

    [code=php]
    function file_open($file, $type, $value = ""){
    if(!file_exists($file) && $type == "a")
    {$type = "w";}
    $fh = @fopen($file, $type);
    if($value != ""){
    @fwrite($fh, $value);
    }
    if($type != 'w' && $type != 'a'){
    return @fread($fh, @filesize($file));
    }
    @fclose ($fh);
    }
    [/code]
    Copy linkTweet thisAlerts:
    @emblemauthorJan 15.2005 — can you break the function down and explain each part and how to use the function because im a compleat newbie in php

    (just barly know varibles)

    i realy apreaciate this jona

    thanks
    Copy linkTweet thisAlerts:
    @JonaJan 15.2005 — [code=php]
    /***
    * Function file_open
    * Purpose: quick way to write to, delete from, or append to a file
    * Variables: $file, $type, $value (optional)
    * Example: file_open(string file to open, string method to open (one of w, w+, r, r+, a, a+, x, x+) [, string value to write to file when using any variation of w, a or x])
    ***/
    function file_open($file, $type, $value = ""){
    # If the file does not exist and you are trying to
    # append to it, create the file instead of returning
    # an error.
    if(!file_exists($file) && $type == "a")
    {$type = "w";}
    # Supress any errors and open the file
    $fh = @fopen($file, $type);
    # If something is to be written to the file
    if($value != ""){
    # Write to the file
    @fwrite($fh, $value);
    }
    # If the file has not been written to
    # and is not supposed to be written to
    if($type != 'w' && $type != 'a'){
    # Read and return the contents of the file
    return @fread($fh, @filesize($file));
    }
    # Close the file; supress errors in the
    # event that the file was never successfully
    # opened.
    @fclose ($fh);
    }
    [/code]


    [font=trebuchet ms]Some example uses...[/font]

    [code=php]
    // Read contents of a file:
    echo file_open('myfile.txt', 'r');

    // Append to a file; create the file and
    // write to it if it doesn't already exist

    $ip = $_SERVER["REMOTE_ADDR"];
    file_open('record.txt', 'a', $ip);

    // Create a file with the
    // contents of "No kidding!"
    file_open('data.txt', 'w', 'No kidding!');
    [/code]
    Copy linkTweet thisAlerts:
    @emblemauthorFeb 07.2005 — thanks alot for the code jona but i dont see any way of clearing the file unless you use one of the letter type thingys

    can you help me on that?

    thanks alto jone a really appreciate it
    ----------


    sorry for the long reply time i messed up my forum acount and could not log in
    Copy linkTweet thisAlerts:
    @JonaFeb 07.2005 — [font=trebuchet ms]Clear the contents of a file:[/font]

    [code=php]
    file_open ('filename.txt', 'w', '');
    [/code]
    Copy linkTweet thisAlerts:
    @emblemauthorFeb 08.2005 — thanks jona
    Copy linkTweet thisAlerts:
    @JonaFeb 08.2005 — [font=trebuchet ms]Happy to help.[/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 07.2005 — hey jona

    are you good in XML?
    Copy linkTweet thisAlerts:
    @JonaMar 07.2005 — [font=trebuchet ms]As in XSLT or XML-processing by a third-party technology? (Such as PHP or JavaScript.)[/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 07.2005 — XML in javascript
    Copy linkTweet thisAlerts:
    @JonaMar 07.2005 — [i]Originally posted by emblem [/i]

    [B]XML in javascript [/B][/QUOTE]


    [font=trebuchet ms]I've done a bit of work with XMLHttpRequest and worked a little with it, but I haven't gotten into data islands or any other things like that. If you have an XML/JavaScript question, you might try asking in the XML or JavaScript forum (whichever is more appropriate).[/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 07.2005 — ok

    i have already posted in the javascript forum

    i just thinking you could help

    ...

    also... im gessing yes but...

    are you good with MYSQL and flatfile Databases

    ...

    sorry if i sound like im nagging
    Copy linkTweet thisAlerts:
    @JonaMar 07.2005 — [font=trebuchet ms]I'm experienced with both MySQL and flatfile databases (particularly with PHP). I don't have a problem with you asking, but I'm curious as to why you are asking. Mind sharing that? ? [/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 08.2005 — i found a pm thing and i wanted to try it out but i found out that you needed a MYSQL database.

    I was thinking it could easly use a Flat file database.

    but as you noticed im not verry good in php so i posted it here:

    http://www.webdeveloper.com/forum/showthread.php?s=&threadid=57712

    can you help me with that that is the whole reason i posted this thread but it was still to complicated.

    Off Topic:

    i looked at your Chatagory Software (sorry for miss spelled words)

    and it is really good
    Copy linkTweet thisAlerts:
    @JonaMar 08.2005 — [font=trebuchet ms]That seems like a lot of work to convert to a flatfile database. You'd have to have a whole system "thingy" to go along with your PM "thingy," and as a result would have to do a lot more converting than just the PM part. Anyway, glad to see you like my Chategory software. ? [/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 21.2005 — hey jona getting back on topic how can you prepend text to a file using this

    thanks again jona
    Copy linkTweet thisAlerts:
    @JonaMar 22.2005 — [i]Originally posted by emblem [/i]

    [B]hey jona getting back on topic how can you prepend text to a file using this

    thanks again jona [/B]
    [/QUOTE

    [code=php]
    file_open('file.txt', 'w', "Something newn".file_open('file.txt', 'r'));
    [/code]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 22.2005 — man your smart ?
    Copy linkTweet thisAlerts:
    @JonaMar 22.2005 — [font=trebuchet ms]Thanks. Sometimes it's not that you're smart, just that you overlooked something very simple. ? [/font]
    Copy linkTweet thisAlerts:
    @emblemauthorMar 22.2005 — i thought it might be somhing like this:

    file_open('file.txt','p','blablan')
    Copy linkTweet thisAlerts:
    @JonaMar 22.2005 — [font=trebuchet ms]It isn't like that because there is no "p" switch. See [url=http://www.php.net/fopen]fopen()[/url].[/font]
    Copy linkTweet thisAlerts:
    @emblemauthorJul 23.2005 — any way well thanks alot for your help it was Extreamly helpful and now because of you i understand fopen/fread ect. (i think).

    SO THANKS x10000 FOR YOUR HELP ?
    ×

    Success!

    Help @emblem spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.7,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...