Jump to content

writing to and reading from a file in one script


juanc

Recommended Posts

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);


[/code][/code][/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.