PHP怎么写入本地文件?

<formaction="processorder.php"method=post><tableborder=0><trbgcolor=#cccccc><tdwidth=... <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>
<td>Shipping Address</td>
<td align=center><input type="text" name="address" size=40 maxlength=40></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="Submit Order"></td>
</tr>
</table>
</form>
<?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/home/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>'; ?></body></html>
展开
 我来答
百度网友0468555
2013-04-01 · TA获得超过2617个赞
知道答主
回答量:963
采纳率:0%
帮助的人:177万
展开全部
你应该是看
PHP和MYSqlWeb开发中文第四版 里面的例子吧,
实际上是这一句出现了问题,
$fp = fopen("$DOCUMENT_ROOT/home/orders/orders.txt", 'ab');
$DOCUMENT_ROOT主要是对这个参数的支持问题,书中的内容是以前的,现在的php版本可能对$DOCUMENT_ROOT的使用有了新的要求,具体上也不很清楚。

你只需要把$DOCUMENT_ROOT修改成为相对路径就行了,比方说保存到当前目录中:
$fp = fopen("./orders.txt", 'ab');

你重新尝试下,应该能成功
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式