juanc Posted August 3, 2006 Share Posted August 3, 2006 Hi therecan 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);[/code][/code][/code] Quote Link to comment Share on other sites More sharing options...
sk93 Posted August 3, 2006 Share Posted August 3, 2006 Hi.May not be the right answer, but it's my understanding of what's written in the manual :)try changing this line:[code]$fh = fopen($file, "w+");[/code]to this:[code]$fh = fopen($file, "r+");[/code]I think this may be the cause of your issue because of the definition of "w+" states:[quote]'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.[/quote]And the "truncate the file to zero length" bit is likely to be returning you an empty string...wheras "r+" only has the following definition:[quote]'r+' Open for reading and writing; place the file pointer at the beginning of the file. [/quote]Hope it works!Cheers,Sk93 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 4, 2006 Share Posted August 4, 2006 write to the file, then close it, then read the contents.[code]fwrite($fh, $_POST['code']);fclose($fh);$data = file_get_contents($file);echo $data;[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.