SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reading from and writing to file in one script problem

    Hi there

    can someone help me here. I'm trying create the same process as on this forum where you can preview the post. Basically I want the contents of a form's textarea to be written to a text file and then output the text file's content to the browser. Below is the code I've used, it works fine as far as writing to the file goes but it doesn't read out to the browser ....ie. echo $data doesn't work. Have I missed something obvious?

    [code]

    $file = "../scratchpad/" . $_POST['theID'] . "/scratchpad.txt";
    $fh = fopen($file, "w+");

    if($_POST['submit'])
    {
    fwrite($fh, $_POST['code']);
    // please not that $_POST['theID'], $_POST['submit'] and $_POST['code'] have all come from a form on another page
    $data = fread($fh,filesize($file));
    echo $data;
    echo "<br /><br />";
    echo "<form action=$_SERVER[PHP_SELF] method=post>";
    echo "<textarea name=code rows=10 cols=50 wrap=virtual>$_POST[code]</textarea><br />";
    echo "<input type=hidden name=theID value=$_POST[theID] />";
    echo "<input type=submit name=submit_update value='Send to scratchpad' />";
    echo "</form>";
    }
    else if(($_POST['submit_update']) or ($_POST['submit_update2']))
    {
    fwrite($fh, $_POST['code']);
    $data = fread($fh,filesize($file));
    echo $data;
    echo "<br /><br />";
    echo "<form action=$_SERVER[PHP_SELF] method=post>";
    echo "<textarea name=code rows=10 cols=50 wrap=virtual>$_POST
    Code:
    </textarea><br />";
    	echo "<input type=hidden name=theID value=$_POST[theID] />";
    	echo "<input type=submit name=submit_update2 value='Send to scratchpad' />";
    	echo "</form>";	   
       }
       
    fclose($fh);

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rewind($fh) before reading


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
  •