Jump to content

problems writing to file with php


robgood

Recommended Posts

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'];

?>

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

 

fwrite($fp, $outputstring, strlen($outputstring));

flock($fp, LOCK_UN);

fclose($fp);

 

echo '<p>Order written.</p>';

?>

</body>

</html>

 

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

 

Link to comment
https://forums.phpfreaks.com/topic/42103-problems-writing-to-file-with-php/
Share on other sites

Hello

 

I am not sure. But I think the warning is from the below line:

 

// open file for appending

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

Sloution:

 

First of all replace the "/" with "\"

Then assigns the file name to one variable like below:

 

$filename = "$DOCUMENT_ROOT\..\orders\orders.txt";

 

echo $filename ; ## Check wether this file exists or not...If it exists, open the file like below:

                        ## File doesnot exists, assigns the correct file location..

 

$fp  = fopen($filename,"a");

 

Regards

Aniesh Joseph

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.