SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Warning: flock() expects parameter 1 to be resource, boolean given ...

    I am a newbie trying to work through a book(Sams - PHP and MySQL Web Development by Welling/Thompson.) I am only on Chapter 2 and am stuck already. I am running some code from their CD and getting this error

    Warning: flock() expects parameter 1 to be resource, boolean given in /Users/me/Sites/php/public_html/02/processorder.php on line 66

    Line 66 is:
    flock($fp, LOCK_EX);


    Any idea what this means? Here is the code I am attempting to run

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

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should do that "if (!$fp)" BEFORE you call flock. Chances are $fp is a boolean FALSE (meaning it failed to open the file) which would explain the error that is occuring on the flock call.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that solved it thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •