note 35739 modified in function.file by danbrown

From: Date: Sun, 01 Mar 2009 18:41:15 +0000
Subject: note 35739 modified in function.file by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151296@lists.php.net to get a copy of this message
Jeff's array2file function is a good start; here are a couple of improvements (no possibility of handle leak when fwrite fails, additional capability of both string2file and array2file; presumably faster performance through use of implode). <?php function String2File($sIn, $sFileOut) { $rc = false; do { if (!($f = fopen($sFileOut, "wa+"))) { $rc = 1; break; } if (!fwrite($f, $sIn)) { $rc = 2; break; } $rc = true; } while (0); if ($f) { fclose($f); } return ($rc); } function Array2File($aIn, $sFileOut) { return (String2File(implode("\n", $aIn), $sFileOut)); } ?> If you're generating your string text using a GET or POST from a TEXTAREA (e.g., a mini-web-text-editor), remember that strip_slashes and str_replace of "/r/n" to "/n" may be necessary as well using these functions. HTH --dir @ badblue com --was-- Jeff's array2file function is a good start; here are a couple of improvements (no possibility of handle leak when fwrite fails, additional capability of both string2file and array2file; presumably faster performance through use of implode). function String2File($sIn, $sFileOut) { $rc = false; do { if (!($f = fopen($sFileOut, "wa+"))) { $rc = 1; break; } if (!fwrite($f, $sIn)) { $rc = 2; break; } $rc = true; } while (0); if ($f) { fclose($f); } return ($rc); } function Array2File($aIn, $sFileOut) { return (String2File(implode("\n", $aIn), $sFileOut)); } If you're generating your string text using a GET or POST from a TEXTAREA (e.g., a mini-web-text-editor), remember that strip_slashes and str_replace of "/r/n" to "/n" may be necessary as well using these functions. HTH --dir @ badblue com http://php.net/manual/en/function.file.php

« previous php.notes (#151296) next »