# Solved: problems writing to file with php



## robgood (Dec 21, 2006)

hi,

Currently working my way through 'PHP and MySQL Web Development' by Luke Welling and Laura Thomson and am having difficulty writing to file in one of the examples.

It throws up the following error:

Warning: fopen(c:\inetpub\wwwroot/../orders/orders.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 64

Here is the script:

<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>

Bob's Auto Parts - Order Results

*Bob's Auto Parts*

*Order Results*

<?php
$date = date('H:i, jS F');

echo '

Order processed at ';
echo $date;
echo '

';

echo '

Your order is as follows:

';

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'
';

if( $totalqty == 0)
{
echo 'You did not order anything on the previous page!
';
}
else
{
if ( $tireqty>0 )
echo $tireqty.' tires
';
if ( $oilqty>0 )
echo $oilqty.' bottles of oil
';
if ( $sparkqty>0 )
echo $sparkqty.' spark plugs
';
}

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

Total of order is '.$totalamount.'

';
echo '

Address to ship to is '.$address.'

';

$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);

fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN); 
fclose($fp);

echo '

Order written.

'; 
?>

Can anyone tell me what i am doing wrong? The book suggests that it is something to do with how the server is set up / permissions? Am running on my own web server (locally)

Many thanks


----------



## MMJ (Oct 15, 2006)

Is that everything? Please put code in [php][/php] tags.


----------



## robgood (Dec 21, 2006)

Yes, thats everyting apart from the html form below:

Bob's Auto Parts

*Bob's Auto Parts*

*Order Form*


ItemQuantityTiresOilSpark PlugsShipping Address 

thanks


----------



## MMJ (Oct 15, 2006)

change line 64 to $fp = fopen("$DOCUMENT_ROOT/orders.txt", 'ab');

It worked for me.


----------



## robgood (Dec 21, 2006)

hi, still getting following message:

Warning: fopen(c:\inetpub\wwwroot/orders.txt) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 64

Warning: flock() expects parameter 1 to be resource, boolean given in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 66

Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 68

Warning: flock() expects parameter 1 to be resource, boolean given in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 69

Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\Learn PHP and MYSQL\Luke Welling\processorder.php on line 70

Could it be to do with my IIS settings?
Have also made sure that safe mode and open_basedir are not enabled in the php settings


----------



## MMJ (Oct 15, 2006)

try

```
$fp = fopen("$DOCUMENT_ROOT\orders.txt", 'ab');
```


----------



## robgood (Dec 21, 2006)

Thanks for all your help MMJ. I did need to specify a different file path from the one in the book, as you suggested (it didn't make a difference using forward or back slashes in the path name as am on windows platform). However, my main problem seemed to be with my IIS permissions. Found the answer to this problem here - http://www.php.net/manual/en/install.windows.iis.php

How to solve the nasty "permission denied" under IIS (Windows xp):

1. Did you disable Simple File Sharing? From any folder click Tools -> Folder Options -> View (tab)-> (scroll the list and find "Simple File Sharing (reccomended)") UNTICK IT
2. Locate your localhost root directory, usually C:\InetPub\wwwroot\
3. Open the Properties of the whole directory, in our case wwwroot
4. Click the Protection tab(where to manage user rights)
5. Click Add and then click Advanced (button in the bottom-left part of the window)
6. Click Find (the only enabled button between Columns and Stop)
7. Pick the user that starts with IUSR_ (which is the IIS default user) from the list Windows has kindly found for you
 8. push OK twice
9. the new added IUSR_ will be selected, from the Authorizations list check Edit (to allow file creation/deletion) and Write (to allow file modify)
10. click OK one more time to get back to your native folder


----------



## MMJ (Oct 15, 2006)

Interesting. 

No Problem.


----------

