Page 1 of 1

fwrite() and fclose() error

Posted: Tue Oct 24, 2006 9:48 pm
by derek barnstorm
Hi, I have done a quick search on this but couldn't find anything.
I am just starting some PHP tutorials but have got stuck with writing to a flat file. It is an order form which should write to a .txt file, I have set the permissions to the directory correctly but I'm still getting errors. Here is the processing script:

Code: Select all

<?php
  // create short variable names
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $address = $_POST['address'];

  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
  <title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
$date = date('H:i, jS F');

echo '<p>Order processed at ';
echo $date;
echo '</p>';

echo '<p>Your order is as follows: </p>';

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';

if( $totalqty == 0)
{
  echo 'You did not order anything on the previous page!<br />';
}
else
{
  if ( $tireqty>0 )
    echo $tireqty.' tires<br />';
  if ( $oilqty>0 )
    echo $oilqty.' bottles of oil<br />';
  if ( $sparkqty>0 )
    echo $sparkqty.' spark plugs<br />';
}

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE
             + $oilqty * OILPRICE
             + $sparkqty * SPARKPRICE;

$totalamount=number_format($totalamount, 2, '.', ' ');

echo '<p>Total of order is '.$totalamount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';

$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                  .$sparkqty." spark plugs\t\$".$totalamount
                  ."\t". $address."\n";

// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);

echo '<p>Order written.</p>'; 
?>
</body>
</html>
This is the link to the php page:
http://kevinblount.com/stuff/processorder.php
And the link to the form, if you need it:
http://kevinblount.com/stuff/orderform.html

I am using PHP 4 on a UNIX server, could this have anything to do with it or could it be a setting in the PHP.ini file?
I am a total newbie to PHP so any help would be great.

Thanks.

Posted: Tue Oct 24, 2006 9:58 pm
by volka
@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
remove the @, maybe php want's to tell you something.

Posted: Tue Oct 24, 2006 10:41 pm
by derek barnstorm
Right, thanks very much... I messed the path to the directory up. :oops:

Thanks a lot.

Posted: Wed Oct 25, 2006 1:37 am
by s.dot
Hope the tutorial you were reading didn't include the @. 8O

Posted: Wed Oct 25, 2006 9:16 am
by derek barnstorm
Yes, it did. I take it that the @ suppresses errors? I think I should have added this after:

Code: Select all

@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

if (!$fp)
{
  echo '<p><strong> Your order could not be processed at this time.  '
       .'Please try again later.</strong></p></body></html>';
  exit;
}
He seems to think that it makes the code harder to debug. The errors tutorials are a few chapters in yet. Would you recommend to never use the @ symbol?

Posted: Wed Oct 25, 2006 11:52 am
by Cameri
Never use the @ symbol, instead make your own error catching system.
The symbol does one thing, suppress errors, but it also makes your code way slower.

A simple way would be this (way too simple?):

Code: Select all

$fp = fopen('path_to_file/file.ext','r') or die("Couldn't open file.\n");
Another way:

Code: Select all

$fp = fopen(...);
if ($fp) {
//work on file
fclose($fp);
} else {
 echo "couldn't open file!";
}

Posted: Wed Oct 25, 2006 3:05 pm
by derek barnstorm
Okay, thanks very much. It looks a lot better, I will practice it in the future.

Thanks again.