"open file for appending" not working

I’m working through the book “PHP and MySQL Web Development” book to keep the mind sharp, I’m retired. The example is Bob’s auto parts where we have an order page that uses the post method to a processorder php page where it opens a file and writes each order on a separate line. The file is kept 2 directory levels back from the directory /var/www/html where the site files live. If there is an error opening or writing, there is a nice error message.

I could not get it to run so I did a chmod 777 on the orders and html directory’s and I know it’s a security no no but it runs now.

I get the error message just like it is supposed to when the file is not written. I can’t get the file to write.

I didn’t type in the code as it is provided on a cd.

Why is the file not being written?
What should the permissions on /orders/orders.txt and /var/www/html be?
Running a LAMP on raspberry Pi.

[php]@ $fp = fopen("$DOCUMENT_ROOT/…/orders/orders.txt", ‘ab’);

flock($fp, LOCK_EX);

if (!$fp) {
  echo "<p><strong> Your order could not be processed at this time.
	    Please try again later.</strong></p></body></html>";
  exit;
}[/php]

Thanks in advance

Fopen’s second parameter should be, “a” not “ab”. That is likely throwing an error.

Unfortunately changing the “ab” to just “a” did not change the result.

remove the error suppressor, the @, and make sure you are showing errors.

What edition is the book? If it is showing you the code with the @ there is probably more wrong stuff it is teaching.

Fourth edition and my guess is that the @ is used because error handling is in a later chapter. Removing it did not change anything, no error message and the text file is not being written. Here is the entire file:

[php]<?php
// create short variable names
$tireqty = $_POST[‘tireqty’];
$oilqty = $_POST[‘oilqty’];
$sparkqty = $_POST[‘sparkqty’];
$address = $_POST[‘address’];
$DOCUMENT_ROOT = $_SERVER[‘DOCUMENT_ROOT’];
$date = date(‘H:i, jS F Y’);
?>

Bob's Auto Parts - Order Results

Bob's Auto Parts

Order Results

<?php
echo "<p>Order processed at ".date('H:i, jS F Y')."</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');

flock($fp, LOCK_EX);

if (!$fp) {
  echo "<p><strong> Your order could not be processed at this time.
	    Please try again later.</strong></p></body></html>";
  exit;
}

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

echo "<p>Order written.</p>";

?>

[/php]

The issues mentioned, are still issues even if they are not the reason for your problem.

Next, check if the file exists and is writable.
[php]$file = “$DOCUMENT_ROOT/…/orders/orders.txt”;
if( file_exists($file) ) {
if( is_writable($file) ){
$fp = fopen($file, ‘a+’);
} else {
echo “

$file is cannot be written.

”;
}
} else {
echo “

$file not found.

”;
}
[/php]

See if one of the else messages appears.

Removing it did not change anything, no error message and the text file is not being written

You need to turn on error reporting.

I miss spoke when I said that it does not write the file, should be "does not create the text file. Running the orderprocessing file gives the “Your order could not be processed at this time. Please try again later.” else message.

Thanks for the help Kevin but I don’t know how to create a database yet, it’s further along in the book, working on text files right now.

Assuming you have made the changes already mentioned, you are probably running into the issue of the system not have permission to create a file in the specified directory.

Kevin never mentioned databases. If you are referring to the PDO portion, that is his signature.

Documentation for fopen

Sponsor our Newsletter | Privacy Policy | Terms of Service