Results 1 to 6 of 6

Thread: [RESOLVED] write to file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] write to file?

    how can i write text into a textbox and then press a button and save the text thats in the textbox to a file?

    edit: i found this code:

    http://www.tizag.com/phpT/filewrite.php

    Code:
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "Floppy Jalopy\n";
    fwrite($fh, $stringData);
    $stringData = "Pointy Pinto\n";
    fwrite($fh, $stringData);
    fclose($fh);
    how can i make this code save from a textbox instead of a string?
    ... and make the linebreaks save with a <br/> tag?

  2. #2
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: write to file?

    input.php
    Code:
    <form action="process.php" method="post">
        <input type="hidden" name="submit" />
        <textarea name="text_input" rows="2" cols="20"></textarea>
        <input type="submit" name="submit" value="Write File" />
    </form>
    process.php
    Code:
    <?php
    if(isset($_POST['submit'])) {
        $myFile = "testFile.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        $stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text-input']);
        fwrite($fh, $stringData);
        fclose($fh); 
    } else {
        header('Location: input.php');
        exit;
    }
    ?>
    Right, so we have input.php, slap this code in the <body> section, you can type info into the textarea and when you hit submit it will send that POST data to the process.php script, it will check to see if the form has been submitted (you can add additional conditions if desired), then replaces the \n or \r with <br/>.

    This code is not tested / written from memory, so don't expect it to work first time. Be sure to tell me how you get on.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: write to file?

    it makes the file but doesn't write the text..

  4. #4
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: write to file?

    My bad, change this:

    Code:
    $stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text-input']);
    to this:

    Code:
    $stringData = str_replace(array("\n","\r"), array("<br/>","<br/>"), $_POST['text_input']);

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: write to file?

    you're referring to $_POST['text-input'] when saving the text with fwrite(); however, your form field is named text_input. simply change that to $_POST['text_input'].

    a somewhat better way of checking for a form submission is checking the REQUEST_METHOD ($_SERVER['REQUEST_METHOD']), by the way. for a small application, it may be un-needed, however.

    edit: oops, I guess you beat me to it.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: write to file?

    i can't belive i missed that one...

    thank you very much.

Posting Permissions

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



Click Here to Expand Forum to Full Width