Page 1 of 1

SOLVED - Newbie - Question from book example

Posted: Wed Sep 07, 2005 5:39 pm
by spyderwoman
I have been reading a book I just bought to teach myself php and JUST STARTED the examples and am already held up...Eventually I took the code straight off the cd to make sure I was doing it right and still get the same error. I am working directly off my off-site server (no lectures please :oops: ). Here is the baby simple code followed by the error I am getting:

Code: Select all

<?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 $DOCUMENT_ROOT;
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/../php/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)); 
fclose($fp);

echo '<p>Order written.</p>'; 
?>
</body>
</html>
Errors:
Warning: fwrite(): supplied argument is not a valid stream resource in D:\root\MYSERVER\www\php\processorder.php on line 75

Warning: fclose(): supplied argument is not a valid stream resource in D:\root\MYSERVER\www\php\processorder.php on line 76

Posted: Wed Sep 07, 2005 6:18 pm
by Weirdan
does $DOCUMENT_ROOT/../php/ exist?

Posted: Wed Sep 07, 2005 6:21 pm
by spyderwoman
yes, i already created that folder on my server. the path appears correct.

Posted: Wed Sep 07, 2005 6:26 pm
by mendingo
If you look at the error, it tells you which line the problem is occuring. In future posts, could you highlight that line, since I don't know how much code is above your segment and don't know for sure where line 75 is.

In this case, though I think the errors are being thrown by these lines:

Code: Select all

fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);
the error is effectively saying that $fp is not a file stream resource, as it needs to be.

$fp is created in this line:

Code: Select all

// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/../php/orders.txt", 'ab');
if this line was working correctly, $fp would be valid, and there would be no errors, so this line appears to be your problem.

However, that line has the @ prefix, which turns off error reporting for the line, so if there is a problem, you've told php not to tell you!

remove the @ from the above line and see what happens. You should get an error on that line, which tells you what's wrong. My guess is that you're using php4.2 or above, which does not automatically extract global variables (look up the "extract" function and superglobals for more info), so $DOCUMENT_ROOT does not exist. If this is the case, you can use $_SERVER['DOCUMENT ROOT'] instead, or (since paths are relative to the current page anyway) just use:

Code: Select all

$fp = fopen("../php/orders.txt", 'ab');
Good luck.

Posted: Wed Sep 07, 2005 6:38 pm
by spyderwoman
That was very detailed and helpful. Thank you! I did what you suggested and removed the @ symbol and saw a new error. Then I edited the fopen line (line 64) as I am using a higher version that 4.2:

Code: Select all

$fp = fopen("../php/orders.txt", 'ab');
However, now I am getting the following error:
Warning: fopen(../php/orders.txt): failed to open stream: Permission denied in D:\root\spyderwoman\spyderwomanweb.com\www\php\processorder.php on line 64
Does this mean I need to edit permissions on my server?
Thanks again for your help.
Cori

Posted: Wed Sep 07, 2005 7:28 pm
by feyd
I would guess you host doesn't allow php access outside the document root (and for good reason), although it may just be a simple permissions thing with that folder, you have to be careful though. Talking about it with your host is probably best for the first go.

Posted: Thu Sep 08, 2005 6:32 am
by spyderwoman
You are right, that was it. Should I move my code to my root rather than a separate folder for security purposes or should is it okay where it is... currently I have it in a folder under my root but I believe they granted permissions to all folders...

Posted: Thu Sep 08, 2005 7:13 am
by s.dot
try

Code: Select all

chmod("yourfile.txt",0777);