RE: [PHP] using fwrite to create PHP files
| From: | Ford, Mike [LSS] | Date: | Tue, 09 Sep 2003 13:02:47 +0000 |
| Subject: | RE: [PHP] using fwrite to create PHP files | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-162057@lists.php.net to get a copy of this message | ||
On 05 September 2003 19:44, Chris Sherwood wrote:
> this is a sample of what I do when I need to write a php file
>
> $stringtowrite = "<?PHP\n// Bulletin Board
> forum\n$"."ForumId=".$tabletofind.";\n";
>
> $stringtowrite .=
>
> "$"."ForumActive='1';\n$"."ForumName='".$sportname."';\n";
>
> $stringtowrite .= "$"."ForumDescription='".$sportname."
> forum';\n";
>
> $stringtowrite .= "$"."ForumConfigSuffix='';\n";
>
> $stringtowrite .= "$"."ForumFolder='0';\n";
>
> $stringtowrite .= "$"."ForumParent='0';\n";
>
> $stringtowrite .= "$"."ForumLang='lang/english.php';\n";
>
> --- and so on until finally
>
> $fd=fopen($final_destination,"w") or die ("file won't open");
> /*if ($fd===false) {
> echo "file create failed";
> exit();
> //return;
> } */
> fwrite($fd,$stringtowrite);
> fclose($fd);
Ouch! This looks like a prime candidate for a heredoc
(http://www.php.net/language.types.string#language.types.string.syntax.heredoc):
$stringtowrite = <<<STRINGTOWRITE
// Bulletin Board forum
\$ForumId=$tabletofind;
\$ForumActive='1';
\$ForumName='$sportname';
\$ForumDescription='$sportname forum';
\$ForumConfigSuffix='';
\$ForumFolder='0';
\$ForumParent='0';
\$ForumLang='lang/english.php';
STRINGTOWRITE;
You can even embed the heredoc directly in the fwrite():
$fd=fopen($final_destination,"w") or die ("file won't open");
fwrite($fd, <<<FILECONTENT
// Bulletin Board forum
\$ForumId=$tabletofind;
\$ForumActive='1';
\$ForumName='$sportname';
\$ForumDescription='$sportname forum';
\$ForumConfigSuffix='';
\$ForumFolder='0';
\$ForumParent='0';
\$ForumLang='lang/english.php';
FILECONTENT
);
fclose($fd);
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211