SOLVED - Newbie - Question from book example

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spyderwoman
Forum Newbie
Posts: 24
Joined: Thu Sep 01, 2005 8:19 pm

SOLVED - Newbie - Question from book example

Post 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
Last edited by spyderwoman on Thu Sep 08, 2005 9:13 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

does $DOCUMENT_ROOT/../php/ exist?
spyderwoman
Forum Newbie
Posts: 24
Joined: Thu Sep 01, 2005 8:19 pm

Post by spyderwoman »

yes, i already created that folder on my server. the path appears correct.
User avatar
mendingo
Forum Commoner
Posts: 28
Joined: Sun May 23, 2004 1:27 pm

Post 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.
spyderwoman
Forum Newbie
Posts: 24
Joined: Thu Sep 01, 2005 8:19 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
spyderwoman
Forum Newbie
Posts: 24
Joined: Thu Sep 01, 2005 8:19 pm

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

try

Code: Select all

chmod("yourfile.txt",0777);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply