How would you write to a file so that it puts the data in the beginning of the file without erasing (overwriting) whats already in the file?
Announcement
Collapse
No announcement yet.
write to file..
Collapse
X
-
PHP Code:<?php
$fp = fopen("myfile.txt","a");
fputs($fp,$data_to_write . "\r\n");
fclose($fp);
?>
additionally, the different php modes:
'r' - Open for reading only; place the file pointer at the beginning of the file.
'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
'w' - Open for writing only; 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.
'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.
'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
-
I'm not sure that's what DsgnrsTLZAdmin's asking.
It sounds to me that instead of adding to the end of the file (appending), he'd like to add to the start of the file. This is not quite as easy a task.
Here is a link that describes the process:
http://unix.about.com/library/tips/bltip056.htm
Somebody here says something about using the rewind function:
http://forums.devshed.com/archive/t-5403
I haven't come up with the exact code in PHP to accomplish your task, however, after reading the above two links you should have a pretty good idea as to what you need to do.
Good luck,
Sadiq.
Comment
-
ok then...
PHP Code:<?php
$result = append_to_top("myfile.txt",$data_to_write);
function append_to_top($data_file,$data) {
/* Store the file's present contents */
$original_data = file_get_contents($data_file);
/*Write the new data to the file, followed by the original data */
$fp = fopen($data_file,"w+");
fputs($fp,$data_to_write . "\r\n");
fputs($fp,$original_data);
fclose($fp);
/* currently returns void */
return();
?>Last edited by Celtboy; Feb 24, 2004, 10:17 PM.
Comment
Comment