Testing PHP on localhost through XAMPP

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Testing PHP on localhost through XAMPP

Postby Brainwave » 21. April 2017 23:51

I am using wamp server. I try to write into the file but it is giving such error: "Warning: fwrite() expects parameter 1 to be resource, boolean given ". How can I solve it?

This is the error i'm getting on my browser:

>Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\processorder.php on line 93

>Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\processorder.php on line 94

This is my code below as well:

<?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 Part - Order Results</title>
</head>
<body>
<h1>Bob's Auto Part</h1>
<form action="processorder.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
<td width="150">Item</td>
<td width="15">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="left"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td align="left"><input type="text" name="oilqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="left"><input type="text" name="sparkqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<tr>
<td>Address</td>
<td align="left"><input type="text" name="address" size="30" maxlength="150" /></td>
</tr>
<td colspan="2" align="center"><input type="submit" value="Submit Order"/> </td>
</tr>
</table>
</form>
<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 'Item 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');
if (!$fp)

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

echo '<p>Order write.</p>';
?>
</body>
</html>

Please any help? The answers i see to the related question does not solve my problem. I will appreciate if anybody can help.
Brainwave
 
Posts: 1
Joined: 21. April 2017 21:15
XAMPP version: v3.2.2
Operating System: 32-bit

Re: Testing PHP on localhost through XAMPP

Postby gsmith » 24. April 2017 00:11

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


Where's the $ in DOCUMENT_ROOT?
@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
gsmith
 
Posts: 211
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win XP to 2012R2/VS 6,9,11,14


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 42 guests