PHP / File Manipulation / Writing to the beginning of a file
Snippet details
ID: 234
Viewed: 2597
Added: 2002-06-10
Version:
Sorry no demo
Very handy to have around. you have a text file that is huge and want to append to it. but you don't want the new data to go clear to the bottom of the file. Well this script will let you do just the opposite. It will let you write to the file but put the new data at the begining and the old data at the end.
General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: unknown
<!---Head--->
none
<!---Body--->
<?
$file_name = "nieuws.txt";
#open and place the $fp point at the beginning of the file
$fp=fopen("$file_name", "r+");
#prevent the file from being messed up.
flock($fp, 2);
#read in the old data
$old_data = fread($fp, filesize($file_name));
# reset the file pointer to the start of the file so that the fwrite overwrites the original data
rewind($fp);
#concat the new data then the old data
fwrite($fp, "new data" . $old_data);
#release the lock, fclose can also do this
flock($fp,3);
#close the file
fclose($fp);
?>
No Reviews to show
