buntus00 Posted July 7, 2009 Share Posted July 7, 2009 I have a working script here in a php file that receives data from a simple form and posts it to a 3rd html file. The script works. As I've toldphp to open the file in 'append' mode, each time data is posted it appends the data, rather than over-writing it, which is what I want, So that works, but I need it to write new data on top. I found a php entity for the job, called 'rewind' which is supposed to set the file pointer to the beginning of the file, so data is written there. Alas, to my perplexity, data is still being appended to the bottom instead of the top. Anyone able to see what's wrong or missing from this simplified snippet? Syntax errors? What have I failed to supply? Thanks. <?php $filename = "jobs/body.html"; $field1="<pre><p><h3>Job Number: "; $field2="<h3>Job title: "; $fp = fopen ($filename, 'a+'); $size = filesize($fn); $hr = "<hr>"; $space = "<br />\r\n"; if ($fp) { rewind($fp); fwrite ($fp, $field1); if($_POST['jobnumber']) fwrite($fp, stripslashes($_POST['jobnumber'])); fwrite ($fp, $field2); if($_POST['jobtitle']) fwrite($fp, stripslashes($_POST['jobtitle'])); fwrite ($fp, $hr); fclose ($fp); echo ("<b>File written successfully!</b>"); } else { echo ("<b>File was not written</b>"); } ?> EDIT: please use code tags next time -zanus Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 7, 2009 Share Posted July 7, 2009 I would use file_get_contents to get the contents of the file, and then rewrite the file with your text on top. Example below: <?php // orig file $contents= file_get_contents("jobs/body.html"); $output = <<<HTML Job Number: {$_POST['jobnumber']} Job Title: {$_POST['jobtitle']} $contents; HTML; $fp = fopen ($filename, 'w+'); fwrite($fp,$output); fclose($fp); ?> Something like that should do the trick. Quote Link to comment Share on other sites More sharing options...
buntus00 Posted July 7, 2009 Author Share Posted July 7, 2009 Thanks for that. However, I understand that rewind($fp); should work. Am I mistaken? I had tried a different switch earlier (r+) and that did write to the start of the file, perfectly, (but that just over-writes the topmost file unfortunately). In your sample script, I understand everything except the $output = <<<HTML and then: $contents; HTML; part. I don't get it. Sorry, I'm a beginner. Do you mean that content /text should go there? Could you explain what's going on there? I mean, you don't mean HTML should be a variable or anything, do you? Also, how would 'file_get_contents' work in this context- bearing in mind there are 3 files involved. File 1: postform.html, that writes to postjob.php which writes the posted data to jobs.html So the data is processed by file 2 and heads straight to file 3. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
p2grace Posted July 7, 2009 Share Posted July 7, 2009 That's heredoc syntax. It's used so you can have double quotes and single quotes without having to escape them. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 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.