Page 1 of 1

creating a HTML file and using fputs/fwrite

Posted: Tue Apr 08, 2003 6:50 am
by deejay
Hi

What I am trying to do is write a script that creates a HTML file. The following code creates the file but I am having a problem putting the content in as it gives me this error
Warning: fwrite(): supplied argument is not a valid File-Handle resource in /php/creatingHTMLoutput.php on line 123
the code is

Code: Select all

<?php
function create_file($file){
$fp = fopen($file, "a");//open file
fwrite($file, "$Content");
fclose($fp);//close file
}//close function
?>
i have also tried fputs, any ideas anyone.


Thanks

Deej

Posted: Tue Apr 08, 2003 7:17 am
by BDKR
The problem is that you are trying to write to a variable as opposed to the file handle. When you open the file with fopen(), you are returned a file handle. In this case, $fp becomes your handle, and therefore fwrite and fputs both will write to it. The line...

Code: Select all

fwrite($file, "$Content");
... should look like ...

Code: Select all

fwrite($fp, $Content);
Notice also that if what is being written to the file is being passed to fwrite as a variable, there is no need for the quotes.

Cheers,
BDKR

Posted: Tue Apr 08, 2003 7:48 am
by deejay
thanks for that, but i still cant put anything in the file. although it doesn't give me ant errors anymore.

the full code i am using is

Code: Select all

<?php
<?php

$Content = 'test text to write to file';


function create_file($file){
$fp = fopen($file, "a");//open file
fputs($fp, $Content);
fclose($fp);//close file
}//close function


?>


<html>

<head>
  <title></title>
</head>

<body>

<?php

create_file("testingOutputWriting.html");//thet will create file testingOutputWriting.html

echo  $Content;

?>

</body>

</html>
?>

from what i have read in other posts I feel this should work. I am grateful fro any help given.

Posted: Tue Apr 08, 2003 8:08 am
by twigletmac
You need to either make $Content global or pass it into the function as an argument (in the same way you did $file), so you could have:

Code: Select all

$Content = 'test text to write to file'; 

function create_file($file){ 
    global $Content;
    $fp = fopen($file, "a");//open file 
    fputs($fp, $Content); 
    fclose($fp);//close file 
}//close function 

create_file("testingOutputWriting.html");
or

Code: Select all

function create_file($file, $Content){ 
    $fp = fopen($file, "a");//open file 
    fputs($fp, $Content); 
    fclose($fp);//close file 
}//close function 

$Content = 'test text to write to file'; 
create_file("testingOutputWriting.html", $Content);
Mac

Posted: Tue Apr 08, 2003 8:24 am
by deejay
doh! think I've been on those stupid pills again. I will make a sign saying 'check globals are put into functions' and put it above my desk.

Thanks

Posted: Tue Apr 08, 2003 8:32 am
by BDKR
Well, let's take it one thing at a time. The first issue is a scope issue. $Content is not global! That said, when you try to echo $Content, the variable, as far as PHP is concerned doesn't exist.

If that sounds confusion, check out the link below.
http://www.php.net/manual/en/language.v ... .scope.php

Moving forward: the function create_file() is not aware of $Content because it's not passed to it in the argument list and it's not made global. Now don't mistake that as you are required to do both of the previous, you are not. You can do one of three things.

a) pass it by like this:

Code: Select all

function create_file($Content, $file)
   {  /* function stuff */ }
b) or:

Code: Select all

function create_file($file)
   {
   global $Content;
   /* function stuff */
   }
c) and:

Code: Select all

function create_file($file)
   {
   /* function stuff */
   fputs($fp, $GLOBALS['Content']);
   /* function stuff */
   }
I would do it like this:

Code: Select all

function create_file($string, $file)
   { 
   $fp = fopen($file, "a");         //open file
   if(!$fp)
      { return false; }
   fputs($fp, $Content);
   fclose($fp);                         //close file
   }
# close function
The perhaps use it as such.

Code: Select all

$Content='Thanx for the ride lady!';
$file='c:\toerag\free_stuff_for_bdkr.html';

create_file(&$Content, $file);

Now in your script, echo'ing $Content will not help you determine that things ran with success. Just go to where the file should be on your disk and see if it's there. You could also use file_exists(). It's all in the manual. :twisted:

Turn your error_reporting all the way up too. Use error_reporint(E_ALL) while testing.

Hope this helps. I know it may seem like confusing theory mumbo jumbo, but it'll help in teh long run.

On another note, I remeber Toerag from a Douglas Adams book. Is that where you came up with the name? Cool stuff. Do you guys do custom printing?

Cheers,
BDKR

Posted: Tue Apr 08, 2003 8:47 am
by deejay
cheers for that, it's all alot clearer in my mind now.

No 'Toerags' hasn't anything to do with Douglas Adams (why hasn't anyone remade 'hitch hickers' for the big screen yet?) , originally because we used to just make socks and its a play on words obviously. Then moved into clothing and used to do some printing but are now moving to just doing the Multi-tools because they are selling well.

Thanks for the help.

Posted: Wed Apr 09, 2003 7:33 am
by deejay
well i thought it was a lot clearer but I am now having this problem.

I have now expanded the value $Content to relate to a function Content() that as well as HTML pulls the latest newsitems from mySQL. when I call this up with

Code: Select all

<?php
echo $Content;  
?>
but now will not print to the file that is being created.

I am using

Code: Select all

<?php
//this function works but not with the above function

function create_file($file){

$fp = fopen($file, "a");//open file
fputs($fp, $GLOBALS['Content']);
fclose($fp);//close file
}//close function


?>


to create the file. I have tried all your suggested ways of doing this except for
I would do it like this:

PHP:


function create_file($string, $file)
{
$fp = fopen($file, "a"); //open file
if(!$fp)
{ return false; }
fputs($fp, $Content);
fclose($fp); //close file
}
# close function

as I don't understand why $string is relevant, I also have not done a file exists check as I will be overwriting a file.

my full code is

Code: Select all

<?php
<?php

error_reporting(E_ALL);

ini_set('display_errors', TRUE);


include_once("../connection/connection.php");

// ******** this function and sql_queries work fine

$newsItemResult = mysql_query("SELECT * FROM mainNewsItem ORDER BY date DESC",$db);

$Content = Content();

function Content()
{

   global $newsItemResult;
   $num_rows = mysql_num_rows($newsItemResult);

 //putting first part of html around my original function

       echo '<html>

	<head>
	<title>My Automatic Updating Newsletter</title>
    </head>

	<body bgcolor="white">
   ';

 //here comes code for writing latest news items

   if ($num_rows > 0 ) { //open if for news

      $i = 0;

      while ($i < 2) {  // open while

      $row = mysql_fetch_assoc($newsItemResult);
      $header = $row['header'];
      $text = $row['text'];
      $author = $row['author'];
      $image = $row['image'];
      $alt = $row['alt'];


         echo '<table border="0" cellspacing="0" cellpadding="10" width="100%">
          <tr>
            <td bgcolor="#CC6666" width="100%"><font class="whiteHeader"><b>'.$header.'
              </b></font></td>
          </tr>
          <tr>
            <td bgcolor="#FFFFFF" width="100%"> <font class="normal"><img src="http://www.cctv-city.com/images/news/new/'.$image.'" align="left" alt="'.$alt.'">
              '.$text.'
              <br>
              <br>
              '.$author.'
              </font></td>
          </tr>
        </table>';
         echo '<br><br>';
         $i++;
      }      //close while

   }      // close if

// here comes the footer of my HTML code for the newsletter
echo  '
	</body>
    </html>';

}
// *********************end of function - is tested in HTML below and works


//this function works but not with the above function

function create_file($file){

$fp = fopen($file, "a");//open file
fputs($fp, $GLOBALS['Content']);
fclose($fp);//close file
}//close function


?>


<html>

<head>
  <title></title>
</head>

<body>

<?php

create_file("testingOutputWriting.html");//thet will create file testingOutputWriting.html

?>
<br>
<?php

// checking that $Content is the value I think it is

echo $Content;

?>

test 7

</body>

</html>

?>
Thank you for any help given.