note 35468 added to function.file

From: Date: Tue, 02 Sep 2003 05:34:50 +0000
Subject: note 35468 added to function.file
Groups: php.notes 
Request: Send a blank email to php-notes+get-55457@lists.php.net to get a copy of this message
<? /** * Takes each element of an array and outputs it to the file with filename "$sFilename" * very much file() in reverse * *@param array The array to write to file *@param string The filename to write out the array to * *@return True on success, False on failure */ function Array2File( $aryArray, $sFilename ){ //Optimistic execution $retVal = true; //Try to open the file if (!$handle = fopen($sFilename, 'wb+')) { print "Cannot open file ($sFilename)"; $retVal = false; }else{ //Convert the array to a string. Each element is one line of the string. $sArrayAsString = ""; foreach( $aryArray as $thisElement ){ $sArrayAsString .= $thisElement . "\n"; } //Be good and remove the trailing newline $sArrayAsString = substr( $sArrayAsString, 0, strlen( $sArrayAsString ) - 1 ); //Write the data to the file if (!fwrite($handle, $sArrayAsString)) { print "Cannot write to file ($sFilename)"; $retVal = false; }else{ //Debug: //print "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } } return $retVal; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.file.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35468 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35468&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35468&report=yes

« previous php.notes (#55457) next »