Hey thanks for the redirect to the question shoonya,
fopen is not a bad place to start if you want to manipulate files. If you read the page on php.net as initially suggested you will find the following code which almost does what you want..
The following cut directly from php.net - posted there by naidim at gmail dot com
While PHP does not have a function to insert text into the middle of a file, it is not that complicated to do.
[PHP]<?php
function addRSSItem($rss File, $firstItem, $item){
// Backup file
if(!copy($rssFi le, 'backup.rss')) die('Backup failed!');
// Store file contents in array
$arrFile = file($rssFile);
// Open file for output
if(($fh = fopen($rssFile, 'w')) === FALSE){
die('Failed to open file for writing!');
}
// Set counters
$currentLine = 0;
$cntFile = count($arrFile) ;
// Write contents, inserting $item as first item
while( $currentLine <= $cntFile ){
if($currentLine == $firstItem) fwrite($fh, $item);
fwrite($fh, $arrFile[$currentLine]);
$currentLine++;
}
// Delete backup
unlink('backup. rss');
}
That's one of those bugging things in PHP. Anyone up for a class that can do it hiding the details so people can use it all the time they want to edit files?
Comment