忙着用一本名为“php和mysql web development 4th edition”的书学习php。我有以下代码的问题。我正在尝试创建一个文本文件。我正在测试实时服务器上的所有代码 我收到以下错误:
Warning: fopen(/home/truevvky/public_html/../orders/orders.txt): failed to open stream: No such file or directory in /home/truevvky/public_html/test/processorder.php on line 60
Warning: flock() expects parameter 1 to be resource, boolean given in /home/truevvky/public_html/test/processorder.php on line 62.
这个想法是创建一个新的文本文件
<?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');
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?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);
?>
</body>
</html>
答案 0 :(得分:1)
打开文件以附加与存在的文件一起使用。我会亲自做一些像
这样的事情$path = "$DOCUMENT_ROOT/../orders/orders.txt";
$content = "Okay here are my contents";
$fp = null;
if(file_exists($path))
{
$fp = fopen($path, 'ab');
}
else
{
$fp = fopen("myText.txt","wb");
}
fwrite($fp,$content);
fclose($fp);
答案 1 :(得分:1)
我正在研究同一本书并找到解决问题的方法。我知道这已经过时了,也许他想出了这个问题,但我想把答案给其他遇到此问题的人。
由于某些原因,在我们甚至知道文件是否存在导致错误的原因之前,他还提到了flock。 flock()只是放一个锁,这样我们就可以编写LOCK_EX和fwrite()然后写入我们输出的内容,LOCK_UN释放锁。
对于$ DOCUMENT_ROOT我的WAMP看起来像这样的“C:/wamp/book/orders/orders.txt”所以在代码中删除“..”。
// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/orders/orders.txt", 'ab');
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
//you move flock down here
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
答案 2 :(得分:1)
也许你没有创造&#34;订单&#34; "$DOCUMENT_ROOT/../"
路径中的文件夹。您应该了解fopen('filepath', 'ab')
只需创建特定文件即可。如果路径未完成(无法找到&#34; orders&#34;文件夹),则无法正常工作。因此,您可以先手动创建文件夹,然后测试.php
答案 3 :(得分:1)
echo $DOCUMENT_ROOT
你可以看到服务器的根地址,我的是D:/ AppServ / www。
如果您使用"$DOCUMENT_ROOT/../orders/orders.txt"
,您将获得地址:D:/AppServ/orders/orders.txt。请注意,您应该确定您有一个名为orders的文件夹。所以我们可以看到..是指文档根目录的父目录,父目录是D:/ AppServ。
答案 4 :(得分:0)
请确保网络服务器用户可以在'/home/truevvky/public_html/../orders/'目录中写入文件。
答案 5 :(得分:0)
fopen声明应为:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'w');
您的方式假定文件存在。
顺便说一下,你在第二个参数中包含的'b'意味着你想用二进制写数据。如果是这种情况,那么fopen应该是:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'wb');