源代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <?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>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Bob's Auto Parts-Order Results</title>
    </head>
    <body>
        <h1>Bob's Auto Pares</h1>
        <h2>Order Results</h2>        <?php
   
         echo "<p>Order processed at".date('H:i,js F Y').'</p>';
         echo "<p>your order is as follow:</p>";
         $totalqty=0;
         $totalqty=$tireqty+$oilqty+$sparkqty;
         echo "Items order:".$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.'oilqty<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 "Address to ship to is ".$address.'</p>';
         $outputstring=$date;
                 
     //open file for appending
    @ $fp=fopen("$DOCUMENT_ROOT/../orders/orders.txt",'b');
      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, $outputsring,strlen($outputstring));
         flock($fp,LOCK_UN);
         fclose($fp);
         echo "<p>Order written.</p>";
      
       ?>    </body>
</html>
运行结果:
Bob's Auto Pares
Order Results
Order processed at14:16,2010 July 2010your order is as follow:Items order:6
1tires
2oilqty
3spark plugsTotal of order is $132.00Address to ship to is 
Warning: flock() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\processorder.php on line 63Your order could not be processed at this time.please try again later.
主要就是文件打不开。没有权限,我用的是xp系统。

解决方案 »

  1.   

    <?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>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Bob's Auto Parts-Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Pares</h1>
    <h2>Order Results</h2>
    <?phpecho "<p>Order processed at".date('H:i,js F Y').'</p>';
    echo "<p>your order is as follow:</p>";
    $totalqty=0;
    $totalqty=$tireqty+$oilqty+$sparkqty;
    echo "Items order:".$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.'oilqty<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 "Address to ship to is ".$address.'</p>';
    $outputstring=$date;//open file for appending
    //确认一下文件路径是不是正确的。
    $fp=fopen("$DOCUMENT_ROOT/../orders/orders.txt",'b');
    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, $outputsring,strlen($outputstring));
    flock($fp,LOCK_UN);
    fclose($fp);
    echo "<p>Order written.</p>";
    ?>
    </body>
    </html>
      

  2.   

    fopen("$DOCUMENT_ROOT/../orders/orders.txt",'b');
    //b方式是什么,好像没记得这个啊 ,用r或者a
      

  3.   

    Warning: fopen(E:/xampp/htdocs/../orders/orders.txt) [function.fopen]: failed to open stream: No such file or directory in E:\xampp\htdocs\processorder.php on line 62
    还是这样。头疼了
      

  4.   

    $fp=fopen("$DOCUMENT_ROOT/../orders/orders.txt",'b'); ???
    b 是二进制方式(linux下不需要)
    你没有给出文件的操作方式据下文,应该是写方式 'wb'另外,你这里加锁文件并无必要,任何访问该页面的用户都将修改文件 orders.txt
    并无冲突可以言
      

  5.   

    谢谢楼上的兄弟。代码加上接过任然不对。加锁代码页删了。还是不行。我刚学,照着《PHP 和mysql web 开发第四版》写的。不知道是不是配置什么没配置好。老是提示没有权限打开文件。
      

  6.   

    路径没有错。放在与processorder.php 相同目录文件夹orders下
      

  7.   

    if (!$fp){
      echo "<p><strong> Your order could not be processed at this time.please try again later.</strong></p></body></html>";
      exit;
      }
    这个信息时你给的,并不是php给出的,所以不一定是文件权限才会!$fp的,你输出$DOCUMENT_ROOT/../orders/orders.txt,然后将输出的内容在文件夹地址栏打开看看,是不是路径根本就不正确
      

  8.   

    Warning: fopen(E:/xampp/htdocs/../orders/orders.txt) [function.fopen]: failed to open stream: No such file or directory in E:\xampp\htdocs\processorder.php on line 62
    这个错误的提示是,没有找到这个文件,或者文件夹,肯定是路径错了!
      

  9.   

    E:/xampp/htdocs/../orders/orders.txt实际地址:E:\xampp\htdocs\orders\orders.txt
      

  10.   

    E:/xampp/htdocs/../orders/orders.txt == E:/xampp/orders/orders.txt 
    实际地址:E:\xampp\htdocs\orders\orders.txt这地址明显的不正确
      

  11.   

    还说你的路径没错?去掉..,这个表示上一级目录
    $fp=fopen("$DOCUMENT_ROOT/orders/orders.txt",'wb');
      

  12.   

    谢谢各位热心的前辈。我会好好学习PHP的。谢谢你们。