Wayne Links
Wayne Links

Reputation: 169

PHP create a text file - difficulty in creating a text file

busy studying php with a book called "php and mysql web development 4th edition". i have trouble with the following code. i am trying to create a text file. i'm testing all code on a live server i get the following errors:

Warning: fopen(/home/truevvky/public_html/../orders/orders.txt): failed to open stream: No such file or directory in /home/truevvky/public_html/test/processorder.php on line 60 

Warning: flock() expects parameter 1 to be resource, boolean given in /home/truevvky/public_html/test/processorder.php on line 62.

the idea is to create a new text file

<?php
    //create short variable names
    $tireqty = $_POST['tireqty'];
    $oilqty = $_POST['oilqty'];
    $sparkqty = $_POST['sparkqty'];
    $address = $_POST['address'];
    $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
    $date = date('H:i, jS F Y');
?>

<html>
    <head>
    <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results</h2>
    <?php           
    echo "<p>Order processed at ".date('H:i, jS F Y')."</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);

            ?>
        </body>
    </html>

Upvotes: 0

Views: 630

Answers (6)

Lying_cat
Lying_cat

Reputation: 1508

echo $DOCUMENT_ROOT

you could see your server's root address, mine is D:/AppServ/www.

If you use "$DOCUMENT_ROOT/../orders/orders.txt" you will get address: D:/AppServ/orders/orders.txt. Notice that you should be sure that you have a file folder named orders. So we can see that .. is to mean the parent directory of the document root directory, the parent directory is D:/AppServ.

Upvotes: 1

STONE-CHN
STONE-CHN

Reputation: 11

Maybe you did not create "orders" folder in "$DOCUMENT_ROOT/../" path. You should understand fopen('filepath', 'ab') just can create the specific file. If the path was not complete (can't find the "orders" folder), it won't work. So you can make the folder manually first, then test the .php

Upvotes: 1

Christopher Ortiz
Christopher Ortiz

Reputation: 359

I'm working on the same book and found the solution to his problem. I know this is old and maybe he figured this out, but wanted to put my answer for anyone else who encounters this.

for some reason in the book he puts flock before we even know if the file exist that's what is causing the error. flock() just puts a lock so that we can write LOCK_EX and fwrite() then writes whatever we output and LOCK_UN releases the lock.

for $DOCUMENT_ROOT mine with WAMP looked like this "C:/wamp/book/orders/orders.txt" so in the code remove the "..".

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

 if (!$fp) {
   echo "<p><strong> Your order could not be processed at this time. 
   Please try again later.</strong></p></body></html>";
   exit;
 }

 //you move flock down here
 flock($fp, LOCK_EX);

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

Upvotes: 1

Amged Rustom
Amged Rustom

Reputation: 1868

The fopen statement should be:

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

Your way assumes the file exists.

By the way the 'b' you included in the second argument means you want to write the data in binary. If that's the case then fopen should be:

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

Upvotes: 0

Taizo Ito
Taizo Ito

Reputation: 424

Please make sure that the web server user can write a file on '/home/truevvky/public_html/../orders/' directory.

Upvotes: 0

Stefano Mtangoo
Stefano Mtangoo

Reputation: 6534

Opening file for appending works with file that exists. I would personally do something like

$path = "$DOCUMENT_ROOT/../orders/orders.txt";
$content = "Okay here are my contents";
$fp = null;
if(file_exists($path))
{
    $fp = fopen($path, 'ab');
}
else
{
    $fp = fopen("myText.txt","wb");
}
fwrite($fp,$content);
fclose($fp);

Upvotes: 1

Related Questions