怎么创建数据存储文件 $fp=fopen("$DOCUMENT_ROOT/../orders/order.txt",'ab')
我学到了数据的存储与检索 书上的一道例题我蒙圈了是关于fopen的<?php
$tireqty=$_POST['tireqty'];
$oilqty=$_POST['oilqty'];
$sparkqty=$_POST['sparkqty'];
$address=$_POST['address'];
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$date=date('H:i, jS F Y');
?>
<html>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Result</h2>
<?php
echo '<p>Order processed at'.date('H:i, jS F Y').'</p>';
echo '<p>Your order is as follow:</p>';
$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.'tireqty<br>';
if($oilqty>0)
echo $oilqty.'bottles of oil<br>';
if($sparkqty>0)
echo $sparkqty.'spark plugs<br>';
}
define('tireprice',100);
define('oilprice',10);
define('sparkprice',4);
$totalamount=$tireqty*tireprice+$oilqty*oilprice+$sparkqty*sparkprice;
echo '<p>Total of order is $'.$totalamount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';
$outputstring=$date."\t".$tireqty."tireqty \t".$oilqty."bottles of oil \t".$sparkqty."sparkqty plugs \t$".$totalamount."\t".$address."\n";
@ $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 writen.</p>';
?>
</body>
html的 我就不写了 点击后的效果 发个图片:
然后点击Submit Order
结果是这样:
Bob's Auto Parts
Order Result
Order processed at09:20, 7th October 2012
Your order is as follow:
Items ordered:0
You did not order anything on the previous page!
Total of order is $0
Address to ship to is
Warning: flock() expects parameter 1 to be resource, boolean given in E:\360Downloads\htdocs\processed.php on line 37
Your order could not be processed at this time.please try again later.
不用说,一定是@ $fp=fopen("$DOCUMENT_ROOT/../orders/orders.txt",'ab');有问题,
$DOCUMENT_ROOT这个应该是服务器端的根目录,可是我不知道怎么去配置或者去创建它,请朋友们指点。
还有这里
Warning: flock() expects parameter 1 to be resource, boolean given in E:\360Downloads\htdocs\processed.php on line 37
这个怎么个意思,也请大家看看
本人自学,遇到问题也就只能问问网上的朋友了,拜托各位了!!!
先谢谢了!!!