On Fri, 23 Feb 2001 08:34, Jan Grafström wrote:
> Hi!
> I am trying to put in a new data at the beginning of a file.
> I have started like this;
>
> --------
>
> $yourfile = "test.xml";  (<a><b><c><d><e><f></c> this is the file.)
> $fp = fopen ($yourfile, r);
> $message  = array(1 => '<a>', '<b>', '<c>', '<d>', '<e>', '<f>',
> '</c>');
> Here I need some code to get the output look like
> this"<a><b><new><c><d><e><f></c>"
>
> print_r($message);
> fwrite ($fp, $message);
> fclose ($fp);
>
> I am thankful for any help.
>
> Regards
> Jan

You need to do something like:
open file for read
while(condition not met)
  read file line by line into array
endwhile
add your element(s) to the array
read the rest of the file into the array
close file
open file for write [which will truncate the file]
write the array to file
close file

You could perhaps enhance this by writing to say a temp file and once you 
are satisfied the write is complete, rename the temp file to the original.

The 'while condition not met' is the test you use to determine when you 
get to the part of the file where you want to add your bits.

Cheers
-- 
David Robley                        | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES  | http://www.nisu.flinders.edu.au/
AusEinet                            | http://auseinet.flinders.edu.au/
            Flinders University, ADELAIDE, SOUTH AUSTRALIA

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to