How do I save a text file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dynamo

    #1

    How do I save a text file?

    Hi all

    I want to save the contents of a textarea within my webform as a text file .txt
    on my webserver. In addition, I want to save it with the same file name as the
    next autoincrement number in my sql table.(e.g. If I have 15 records in my table
    then the next autoincrement number will be 16 when I add a new record and I want
    the text file to be saved as 16.txt)

    sadly this is far as I have got cos im not sure how to do either of the above.

    <?php
    $textfileconten ts=$_POST['text'];
    ?>

    Any help greatly appreciated

    Regards
    Dynamo

  • Roy W. Andersen

    #2
    Re: How do I save a text file?

    Dynamo wrote:[color=blue]
    > I want to save the contents of a textarea within my webform as a text file .txt
    > on my webserver. In addition, I want to save it with the same file name as the
    > next autoincrement number in my sql table.(e.g. If I have 15 records in my table
    > then the next autoincrement number will be 16 when I add a new record and I want
    > the text file to be saved as 16.txt)[/color]

    If you're actually putting it into the table, you can get the ID by
    calling mysql_insert_id () after inserting it into the table.

    So something like this should work:

    function insert_and_writ e($data) {
    if (mysql_query("I NSERT INTO `table` (fieldname) VALUES ('$data')",
    $link)) {
    $filename = mysql_insert_id ().".txt";
    $fp = fopen($filename , 'w');
    fwrite($fp, $data);
    fclose($fp);
    return true;
    }
    return false
    }

    This will also prevent it from trying to write to the file if the INSERT
    query fails. I haven't tested this, but I'm pretty sure mysql_query()
    returns false if the query fails for some reason.


    Roy W. Andersen
    --
    ra at broadpark dot no / http://roy.netgoth.org/

    "Hey! What kind of party is this? There's no booze
    and only one hooker!" - Bender, Futurama

    Comment

    • Erwin Moller

      #3
      Re: How do I save a text file?

      Dynamo wrote:
      [color=blue]
      > Hi all
      >
      > I want to save the contents of a textarea within my webform as a text file
      > .txt on my webserver. In addition, I want to save it with the same file
      > name as the next autoincrement number in my sql table.(e.g. If I have 15
      > records in my table then the next autoincrement number will be 16 when I
      > add a new record and I want the text file to be saved as 16.txt)
      >
      > sadly this is far as I have got cos im not sure how to do either of the
      > above.
      >
      > <?php
      > $textfileconten ts=$_POST['text'];
      > ?>
      >
      > Any help greatly appreciated
      >
      > Regards
      > Dynamo[/color]

      Hi Dynamo,

      Wouldn't it be easier for you to store that text in the database itself?

      That aside.

      This is how to do that:
      1) Get the next autoincrement.
      (I have no clue about the excact syntax because you din't mention the
      database.)

      For now I take that you find that number and store it in $ai, ok?

      2) Create a new file with the right name ($ai.txt).
      to do this:
      - go to www.php.net
      - look for the function fopen. (And when you are there, you can find all I
      write here too, better, with more examples)

      resource fopen ( string filename, string mode [, bool use_include_pat h [,
      resource zcontext]])

      So:
      $filename = $ai.".txt";
      $dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
      $fullpath = $dir.$filename;
      $handle = fopen ($fullpath, "w+");

      // now write
      fwrite($handle, $_POST["text"]);

      // and be a nice and close:
      fclose($handle) ;


      It is all on www.php.net
      This is a good startingpoint:


      Please note that I didn't implement any errorhandling.
      But you should.

      Regards,
      Erwin Moller

      PS: I didn't test the code.

      Comment

      • Dynamo

        #4
        Re: How do I save a text file?

        In article <41e5439c$0$621 8$e4fe514c@news .xs4all.nl>, Erwin Moller says...[color=blue]
        >Hi Dynamo,
        >
        >Wouldn't it be easier for you to store that text in the database itself?
        >
        >That aside.
        >
        >This is how to do that:
        >1) Get the next autoincrement.
        >(I have no clue about the excact syntax because you din't mention the
        >database.)
        >
        >For now I take that you find that number and store it in $ai, ok?
        >
        >2) Create a new file with the right name ($ai.txt).
        >to do this:
        >- go to www.php.net
        >- look for the function fopen. (And when you are there, you can find all I
        >write here too, better, with more examples)
        >
        >resource fopen ( string filename, string mode [, bool use_include_pat h [,
        >resource zcontext]])
        >
        >So:
        >$filename = $ai.".txt";
        >$dir = "/home/dynamo/myfiles/"; // adjust to where you want to save
        >$fullpath = $dir.$filename;
        >$handle = fopen ($fullpath, "w+");
        >
        >// now write
        >fwrite($handle , $_POST["text"]);
        >
        >// and be a nice and close:
        >fclose($handle );
        >
        >
        >It is all on www.php.net
        >This is a good startingpoint:
        >http://nl3.php.net/manual/en/ref.filesystem.php
        >
        >Please note that I didn't implement any errorhandling.
        >But you should.
        >
        >Regards,
        >Erwin Moller
        >
        >PS: I didn't test the code.[/color]

        Thanks for that. Tried it and now working OK

        Comment

        Working...