Jump to content

Appending data...to start of file


buntus00

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.