In PHP, you can easily open a file for writing, creating the file from scratch, even if it already exists, or for appending, adding to the end of a file. There doesn't exist a way to easily add content into the middle of a file. However, with a few simple functions, it isn't that hard to do.
One site I work on adds MP3s to a podcast every week. Instead of manually adding the new MP3 each week, or recreating the podcast with some software package, I wanted a PHP script to automatically update the RSS file after uploading the MP3. To do that, this is the function I created.
Insert RSS Item
You first must know the location of the file, the line where the header stops and the items begin, and the content you want to add. The function takes this information and processes the RSS file.
The first thing the function does is make a backup of the file (in case an error occurs) then take the existing RSS file and puts it into an array.
<?php
function addRSSItem($rssFile, $firstItem, $item){
// Backup file
if(!copy($rssFile, 'backup.rss')) die('Backup failed!');
// Store file contents in array
$arrFile = file($rssFile);
}
?>
Once it has the current file in an array, it opens the file for writing, completely erasing the existing file (but we have the backup, and the contents in our array). We also set our line counter variable, $currentLine, and total line count, $cntFile.
<?php
function addRSSItem($rssFile, $firstItem, $item){
// Backup file
if(!copy($rssFile, '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);
}
?>Now we need to write our file lines (from $arrFile) back to our file, inserting our new item, $item, before the first item line, making our new item the first item. After a successful file creation, we also delete our backup file.
<?php
function addRSSItem($rssFile, $firstItem, $item){
// Backup file
if(!copy($rssFile, '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');
}
?>
While this function was designed for my RSS feed needs, it can be easily modified to insert any data at any line in any file.
Lastly, here's an example of our function in use:
<?php
function addRSSItem($rssFile, $firstItem, $item){
// Backup file
if(!copy($rssFile, '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');
}
$data = " <item>\n<title>$_POST['title]</title>\n".
" <description>$_POST['description']</description>\n <pubDate>$_POST['date']</pubDate>\n".
" <link>http://www.site.com/mp3s/".basename($_FILES['fullPath']['name'])."</link>".
" <enclosure url=\"http://www.site.com/mp3s/".basename($_FILES['fullPath']['name']).
"\" length=\"$_FILES[fullPath][size]\" type=\"audio/mpeg\" />".
" </item>\n";
addRSSItem('/var/www/vhosts/site.com/httpdocs/rss/podcast.rss',20,$data);
?>

