fopen permission denied

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

 I'm following along in the book "PHP & MySQL Web Development" by Luke
Welling and Laura Thompson. And currently I'm trying to store some data in
a text file (I'm going to go over database storage sometime later).

I'm using this statement in my code to write to the text file:

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

Which corresponds with this file on my web server:

[root@web1:~] #ls -l /var/www/php-webdev/orders/orders.txt

-rwxr-xr-x. 1 apache apache 0 Dec 27 21:47
/var/www/php-webdev/orders/orders.txt

And I'm getting permission denied when I try to access this file. And no
data is written to it:

*Warning*: fopen(/var/www/php-webdev/orders/orders.txt): failed to open
stream: Permission denied in */var/www/php-webdev/ch01/processorder.php* on
line *50*


This result is a little bit confusing. Because the permissions look right
to me.

I'm running httpd as the apache user and group:

[root@web1:/etc/httpd] #egrep -i "user|group" conf/httpd.conf  | grep -i -v
-e logformat -e '#'
User apache
Group apache

And my ownership and permissions look right to me all the way up the file
hierarchy:

[root@web1:/etc/httpd] #ls -ld /var/www
drwxr-xr-x. 13 apache apache 4096 Dec 27 21:39 /var/www

[root@web1:/etc/httpd] #ls -ld /var/www/php-webdev/
drwxr-xr-x. 7 apache apache 81 Dec 27 21:47 /var/www/php-webdev/

[root@web1:/etc/httpd] #ls -ld /var/www/php-webdev/orders/
drwxr-xr-x. 2 apache apache 23 Dec 27 21:47 /var/www/php-webdev/orders/

[root@web1:/etc/httpd] #ls -lh  /var/www/php-webdev/orders/orders.txt
-rwxr-xr-x. 1 apache apache 0 Dec 27 21:47
/var/www/php-webdev/orders/orders.txt

So I'd like to try to find out why this is still not working. I've even
found if I (very temporarily) try and open up the entire directory
structure to world readable, I get the same failure. I only tried this long
enough to test!

Below, I'll show my code for both the HTML form and the php script it
refers to, in the hopes someone out there can find a clue as to why this is
failing:

*HTML FORM:*

<!doctype html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>Order Form</title>
</head>
<body>
<form action="processorder.php" method="post" id="usrform">
    <table border="0">
        <tr bgcolor="#cccccc">
            <td width="150">Item</td>
            <td width="15">Quantity</td>
        </tr>
        <tr>
            <td>Tires</td>
            <td align="center"> <input type="text" name="tireqty" size="3"
maxlength="3" /></td>
        <tr>
        <td>Oil</td>
        <td align="center"><input type="text" name="oilqty" size="3"
maxlength="3" /></td>
        </tr>
        <tr>
            <td>Spark Plugs</td>
            <td align="center"><input type="text" name="sparkqty" size="3"
maxlength="3" /></td>
        </tr>
        <tr>
            <td>Address</td>
            <td><textarea cols="40" rows="5" name="address">
                Please enter your address.
            </textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit"
value="Submit Order" /></td>
        </tr>


        <tr>
            <td>How did you find Bob’s?</td>
            <td><select name="find">
                <option value="a">I’m a regular customer</option>
                <option value="b">TV advertising</option>
                <option value="c">Phone directory</option>
                <option value="d">Word of mouth</option>
                <option selected="selected" value="e">Don't know</option>
            </select>
            </td>
        </tr>


    </table>
</form>
</body>

*PHP Script:*

<?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');
echo $DOCUMENT_ROOT;
$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>

The actual form and script can be found here, hanging out on the web:

http://php.lyricgem.com/ch01/orderform.html

Definite thanks for any assistance and observations you can provide!

Thank you,
Tim

-- 
GPG me!!

gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux