Trouble to use fopen()

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
cyh123
Forum Newbie
Posts: 4
Joined: Wed Oct 17, 2007 5:51 am

Trouble to use fopen()

Post by cyh123 »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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)

Code: Select all

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

Need your kind help. TQ So Much


Details content as below:

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

Code: Select all

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

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Trouble to use fopen()

Post by neophyte »

Have your tried seeing if the file exists? file_exists()

Make sure your paths are correct. And then double check to make sure the web user (IUSER_MACHINE ... I think) has access to your file system.

Do you have register globals on? What does $DOCUMENT_ROOT contain?

Cheers!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Remove the @ error suppressor. Is error reporting on?
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.
cyh123
Forum Newbie
Posts: 4
Joined: Wed Oct 17, 2007 5:51 am

Trouble to use fopen()

Post by cyh123 »

TQ for all the suggestion

The @ removal - no help. thks

Simplify the code to ===> $fp = fopen("orders.txt", 'ab');

a) When run under IIS http://localhost/02/processorder.php
==> Still Cannot create file....????


b) When run by double click the php file (processorder.php)
==> Can create the file (orders.txt)


** Suspect need to configure IIS system to allow file access permission, pls suggest me how to do it?

** Any different for IIS file permission set under windows server 2003 and windows XP....?


Kindly advice

TQVM
Post Reply