note 74182 added to function.fopen

From: Date: Wed, 28 Mar 2007 22:54:48 +0000
Subject: note 74182 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-124901@lists.php.net to get a copy of this message
While PHP does not have a function to insert text into the middle of a file, it is not that complicated to do. <?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/html/rss/podcast.rss',20,$data); ?> ---- Server IP: 216.194.113.175 Probable Submitter: 166.89.26.43 ---- Manual Page -- http://www.php.net/manual/en/function.fopen.php Edit -- https://master.php.net/note/edit/74182 Del: integrated -- https://master.php.net/note/delete/74182/integrated Del: useless -- https://master.php.net/note/delete/74182/useless Del: bad code -- https://master.php.net/note/delete/74182/bad+code Del: spam -- https://master.php.net/note/delete/74182/spam Del: non-english -- https://master.php.net/note/delete/74182/non-english Del: in docs -- https://master.php.net/note/delete/74182/in+docs Del: other reasons-- https://master.php.net/note/delete/74182 Reject -- https://master.php.net/note/reject/74182 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124901) next »