Trouble During fopen()

Discussion in 'PHP' started by cyh123, Oct 17, 2007.

  1. #1
    Dear

    Facing trouble when using fopen() to open a file, maybe do not have the write access to create file or ..... I'm using PHP5.2.4 under IIS5.1.

    (File No 2)
    @ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');


    Need your kind help. TQ So Much


    Details content as below:

    @@@@@@@@ File No 1 @@@@@@@@@@
    orderform.html

    <html>
    <head>
    <title>Bob's Auto Parts</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Form</h2>

    <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>

    </body>
    </html>



    @@@@@@@@ File No 2 @@@@@@@@@@
    processorder.php

    <?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/../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>
     
    cyh123, Oct 17, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What is the exact error you get? If the file does not exist, try to create it manually. Or set the file permissions to 777.

    And please post your code between [html] [/html] or [php] [/php] tags in the future. It's way easier to read this way.
     
    nico_swd, Oct 17, 2007 IP