Jump to content

beginner having trouble with BASIC fopen()


ivey

Recommended Posts

Hi there,

 

i don't know why this is so difficult. i just accessed apache and php on my mac. i'm going through Luke Welling and Laura Thomsons' PHP and MySQL Web Development. It's the Bob's Auto Parts bit...

 

anwyay, the first problem was that when i tried to use fopen() it would not create a new file that didn't already exist. so i just tried putting the file where it needed to be manually, but now it still won't write to it. what's my problem?

 

oh yes, and i checked the permission for the file i created manually and it is 644. so i tried to chmod it, but it wouldn't let me chmod it. so i tried to chown it, but that didn't work either. i got the following:

 

Warning: chown() [function.chown]: Operation not permitted in /Users/ivey/Sites/phppractice/processor.php on line 22

 

Warning: chmod() [function.chmod]: Operation not permitted in /Users/ivey/Sites/phppractice/processor.php on line 22

 

here's the code i'm using:

 

<?php

//create short variable names

$tire = $_POST['tire_qty'];

$oil = $_POST['oil_qty'];

$spark = $_POST['spark_qty'];

$total_qty = 0;

$total_amount = 0.00;

$address = $_POST['address'];

 

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

 

define('TIREPRICE', 100);

define('OILPRICE', 10);

define('SPARKPRICE', 4);

?>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<h2>Bob's Auto Parts</h2>

<h3>Order Results</h3>

<?php

 

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

echo '<p> Order results at ';

echo $date;

echo '</p>';

 

echo '<p> Your order is as follows:</p>';

 

$total_qty = $tire + $oil + $spark;

echo 'Items ordered: '.$total_qty.'<br />';

 

$total_amount = $tire * TIREPRICE

+ $oil * OILPRICE

+ $spark * SPARKPRICE;

 

 

$total_amount=number_format($total_amount, 2, '.', ' ');

echo '<p>Total of order is '.$total_amount.'</p>';

echo '<p>Address to ship to is '.$address.'</p>';

 

$file = 'orders/orders.txt';

chown($file, nobody);

chmod($file, 0777);

 

//opening file 

 

$outputstring = $date."\t".$tire." tires \t".$oil." oil \t".$spark." spark plugs\t$".$total_amount."\t". $address."\n";

 

//open file for appending

 

 

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

 

if(!$fp)

{

echo '<p><strong>Your order could not be placed at this time. '

.'Please try again.</strong></p></body></html>';

exit;

}

 

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

fclose($fp);

 

 

 

?>

 

 

</body>

</html>

 

 

thanks so much for your help!!!!

Link to comment
Share on other sites

i did try changing the permission, but it wouldn't let me.

 

is this some kind of a mac problem?

 

and, by the way, the php manual says specifically that 'a' is supposed to append a file if it is there and CREATE it if it is not there. why won't it just create a file? if i delete the file i created manually, it still gives me the error. i feel like i'm going insane. does this work for other people?

 

thanks so much for your help. i've been messing with this for hours...

 

i

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.