fwrite() and fclose() error

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
derek barnstorm
Forum Commoner
Posts: 36
Joined: Thu May 18, 2006 11:23 am

fwrite() and fclose() error

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
remove the @, maybe php want's to tell you something.
derek barnstorm
Forum Commoner
Posts: 36
Joined: Thu May 18, 2006 11:23 am

Post by derek barnstorm »

Right, thanks very much... I messed the path to the directory up. :oops:

Thanks a lot.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Hope the tutorial you were reading didn't include the @. 8O
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
derek barnstorm
Forum Commoner
Posts: 36
Joined: Thu May 18, 2006 11:23 am

Post 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?
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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!";
}
derek barnstorm
Forum Commoner
Posts: 36
Joined: Thu May 18, 2006 11:23 am

Post by derek barnstorm »

Okay, thanks very much. It looks a lot better, I will practice it in the future.

Thanks again.
Post Reply