orderform.html

<!DOCTYPE html>
  <html>
    <head>
        <title>Bob's Auto Parts - Order Form</title>
    </head>
    <body>

      <form action="processorder.php" method="post">
        <table style="border: 0px;">
        <tr style="background: #cccccc;">
          <td style="width: 150px; text-align: center;">Item</td>
          <td style="width: 15px; text-align: center;">Quantity</td>
        </tr>
        <tr>
          <td>Tires</td>
          <td><input type="text" name="tireqty" size="3" maxlength="3"></td>
        </tr>
        <tr>
          <td>Oil</td>
          <td><input type="text" name="oilqty" size="3" maxlength="3"></td>
        </tr>
        <tr>
          <td>Spark Plugs</td>
          <td><input type="text" name="sparkqty" size="3" maxlength="3"></td>
        </tr>
        <tr>
          <td>Shipping Address</td>
          <td><input type="text" name="address" size="50" maxlength="100"></td>
        </tr>
        <tr>
          <td colspan="2" style="text-align:center;"><input type="submit" value="Submit Order"></td>
        </tr>
      </form>
    </body>
  </html>

processorder.php

<?php
  // create short variable names
  $tireqty = (int) $_POST['tireqty'];
  $oilqty = (int) $_POST['oilqty'];
  $sparkqty = (int) $_POST['sparkqty'];
  $address = preg_replace('/\t|\R/',' ',$_POST['address']);
  $document_root = $_SERVER['DOCUMENT_ROOT'];
  $date = date('H:i, jS F Y');
?>
<!DOCTYPE html>
<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;
      $totalamount = 0.00;

      define('TIREPRICE', 100);
      define('OILPRICE', 10);
      define('SPARKPRICE', 4);

      $totalqty = $tireqty + $oilqty + $sparkqty;
      echo "<p>Items ordered: ".$totalqty."<br />";

      if ($totalqty == 0) {
        echo "You did not order anything on the previous page!<br />";
      } else {
        if ($tireqty > 0) {
          echo htmlspecialchars($tireqty).' tires<br />';
        }
        if ($oilqty > 0) {
          echo htmlspecialchars($oilqty).' bottles of oil<br />';
        }
        if ($sparkqty > 0) {
          echo htmlspecialchars($sparkqty).' spark plugs<br />';
        }
      }


      $totalamount = $tireqty * TIREPRICE
                   + $oilqty * OILPRICE
                   + $sparkqty * SPARKPRICE;

      echo "Subtotal: $".number_format($totalamount,2)."<br />";

      $taxrate = 0.10;  // local sales tax is 10%
      $totalamount = $totalamount * (1 + $taxrate);
      echo "Total including tax: $".number_format($totalamount,2)."</p>";

      echo "<p>Address to ship to is ".htmlspecialchars($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/PHP-and-MySQL-Web-Development/orders/orders.txt", 'w');

       if (!$fp) {
         echo "<p><strong> Your order could not be processed at this time.
               Please try again later.</strong></p>";
         exit;
       }

       flock($fp, LOCK_EX);
       fwrite($fp, $outputstring, strlen($outputstring));
       flock($fp, LOCK_UN);
       fclose($fp);

       echo "<p>Order written.</p>";
    ?>
  </body>
</html>

document root is /var/www/html/
$document_root/PHP-and-MySQL-Web-Development/orders/oders.txt
folder orders/orders.txt
orders.txt is chmod 777 and albert albert is not root the file property

Warning: fopen(/var/www/html/PHP-and-MySQL-Web-Development/orders/orders.txt): Failed to open stream: Permission denied in /var/www/html/PHP-and-MySQL-Web-Development/processorder.php on line 64
without at $fp appear this error

chcon -R -t httpd_sys_rw_content_t /var/www/html/PHP-and-MySQL-Web-Development/orders

bertrc Permission denied...

Sounds like either you need to add write permission to the target directory so that the web server user (which likely is not the same user you use to administer the site) has write permissions, and/or to the specific file you want to write to.

Also, w.r.t. this:

       // open file for appending
       @$fp = fopen("$document_root/PHP-and-MySQL-Web-Development/orders/orders.txt", 'w');

The 'w' option will overwrite the existing file, not append to it. If you truly want to append, then use the 'a' option, instead. Or if you go for the suggestion to use file_put_contents(), then add the constant FILE_APPEND as the optional 3rd argument to that function.

If you would like to find out the user under which the system runs apache, you can put this file in your server's web root and access it with a browser:

<?php
passthru('whoami');

To change the owner of a directory on linux, you use the chown command. To change the read/write/execute permissions, you use the chmod command.

    Write a Reply...