Ok, so if I understand how to use the dirname() function... it should be like this:
Code: Select all
$path = "learnphp/orders.txt";
$filepath = dirname($path);
echo "$filepath";
$filepath then returns this:
I am starting to think that this problem may be a little above my head - the problem could possibly rest upon the server I'm on and not my code... is that possible? If so, I don't know what to do. However, I did try something. instead of adding just $filename to the first argument passed to fopen() - I added this:
Code: Select all
@ $fp = fopen("$filepath"."orders.txt", 'a');
Which should result in this string: learnphp/var/www/orders.txt
With that entered, the error message dissapeared
(which makes me think that the file has been opened!) ... but my $outputsrting was not added to orders.txt. This leaves me completely stumped. So what I'm asking for now - are not little hints, but full on 'you do it for me' help. The book I am following with this, used $_SERVER['DOCUMENT_ROOT']. It would seem this has become way overcomplicated.
EDIT: For completeness - I will post the code of processorder.php, which is the page that I am working on atm. This page, takes input from a form and builds upon it - but again, the part that isn't working would be me trying to write the order to a file.
Code: Select all
<html>
<head>
<title>Order Processing.</title>
</head>
<body>
<?php
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
/* This scripts interprets, and builds upon,
the information entered into the form that is
found on the index.php page. */
//changes medium variable names to small names.
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F');
//tallies up the total amount of items
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
if ($totalqty == 0)
{
echo 'You did not order any items! <br />';
}
else
{
echo 'Items ordered: '.$totalqty.' <br />';
echo 'You have ordered: <br />';
//outputs form values
echo "$tireqty Tires <br />";
echo "$oilqty Oil Cans <br />";
echo "$sparkqty Spark Plugs <br />";
$totalamount = 0.00;
//needed constants
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
//tallies up total cost
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;
/* this statement outputs the cost, without tax
The number_format() function is a php math
command that sets decimal places on the number
passed to the function */
echo 'Subtotal: $'.number_format($totalamount,2).'<br />';
//calculates the cost - with tax.
$taxrate = 0.09;
$totalamount = $totalamount * (1 + $taxrate);
echo 'Total including tax: $'.number_format($totalamount,2).'<br />';
echo 'Shipping Address: '.$address.'<br />';
//attempts to open, and then write to a file.
$path = "learnphp/orders.txt";
$filepath = dirname($path);
@ $fp = fopen("$filepath"."orders.txt", 'a');
if (!$fp)
{
echo '<p><strong> Your order could not be processed at
this time. '
.'Please try again later.</strong></p>';
exit;
}
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
.$sparkqty." spark plugs\t\$".$totalamount
."\t". $address."\n";
fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);
echo '<p>Order Written to file</p>';
}
?>
</body>
</html>