format output using fwrite function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

format output using fwrite function

Post by mesz »

I am trying to format a newly created file by send ing a template to be written at the same time as a I send the data to be written to a file.
However the file still appears as a basic text window, and whats worse I lose the data that is entered - any idea why?

Code: Select all

<?PHP
$filename='/home/.sites/14/site467/web/test/test1/'.$thename .'.php';
$fp = fopen('list.dat',"a+");
  $line .= "|" . $HTTP_POST_VARS['thename'];
$line .= "\r\n";
fwrite($fp,$line);
fclose($fp);
$hoog = '/home/.sites/14/site467/web/template.php';
$Template = fopen($hoog, "r"); 
$fw = fopen($filename ,'w+');
  $line1 .= "|" .  $HTTP_POST_VARS['news'];
$line1 .= "\r\n";
fwrite($fw ,$line1, $Template);
fclose($fw);
?>
Only the first 3 characters of the $news variable are written to th newly created file.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

This was so very not working that I tried another tack..

Code: Select all

<?PHP
require('/home/.sites/14/site467/web/template.php');
?>
<div class="main_col_L">
<font class="wwi">
<?PHP
$filename='/home/.sites/14/site467/web/test/test1/'.$thename .'.php';
$fp = fopen('list.dat',"a+");
  $line .= "|" . $HTTP_POST_VARS['thename'];
$line .= "\r\n";
fwrite($fp,$line);
fclose($fp);

$CallFunction=WriteToFile 
("<tr><td align="center">".$thename."</td></tr> 
<tr><td bgcolor="#0C2450"><img src="/home/.sites/14/site467/web/m.gif" height="15"></td></tr> 
<tr><td> 
<table bgcolor="#CCCCCC" border="0" align="center" cellpadding="0" cellspacing="0" width="500"> 
<tr align="center"><td align="left" width="500" class="content">" 
.$news." 
</td></tr>" 
); 
function WriteToFile($centerCopy)
{ 
$fw=fopen($filename, "a+"); 
fwrite ($fw, "$centerCopy\n"); 
fclose ($fw); 
} 
?>
I now have proper errors and everything:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 26

Warning: fclose(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 27

:!:
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

globalize the $filename variable

function WriteToFile($centerCopy)
{
global $filename;
$fw=fopen($filename, "a+");
fwrite ($fw, "$centerCopy\n");
fclose ($fw);
}
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

or give it to the function

function WriteToFile($centerCopy,$filename)
{

$fw=fopen($filename, "a+");
fwrite ($fw, "$centerCopy\n");
fclose ($fw);
}
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Tubbietoeter,
Thank you very much - this works to perfection.
:lol: & :D & also :P
Post Reply