fopen()

I must be an idiot

Page 1 of 1

8 Replies - 1501 Views - Last Post: 15 February 2009 - 03:19 PM Rate Topic: -----

#1 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

fopen()

Posted 14 February 2009 - 09:22 PM

I have been working all day in this book and getting no where...

everytime I submit I get the message that is programmed near the bottom that is supposed to pop up if there is an error.

The forms information never stores in the .txt file like it is suppose to so I am assuming I have the fopen() folders not installed right

right now I am using wamp server, I have my form orderform.html and processorder.php (this file below) in a folder called webserver. Also in the webserver folder is a folder called orders. Inside the orders folder is my orders.txt file where I am trying to store data

<?php



	$tireqty = $_POST['tireqty'];
	$oilqty = $_POST['oilqty'];
	$sparkqty = $_POST['sparkqty'];
	$address = $_POST['address'];
 
	
	echo '<p>Order processed at ';
	echo date('H:i, jS F'); 
	echo '</p>';
	

	
	
	echo '<p>Your order is as follows: </p>';
	echo $tireqty.' tires<br />';
	echo $oilqty.' bottles of oil <br />';
	echo $sparkqty.' Spark Plugs <br />';
	
	$totalqty =0;
	$totalqty= $tireqty + $oilqty +$sparkqty;
	echo 'Items ordered: '.$totalqty.'<br />'; //total number of items ordered
	
	$totalamount = 0.00; //total amount before tax
	
	
	define('TIREPRICE', 100);
	define('OIL', 10);
	define('SPARKPRICE', 4);
	
	$totalamount = $tireqty * TIREPRICE
				+ $oilqty * OILPRICE
				+ $sparkqty * SPARKPRICE;
				
	echo 'Subtotal: $'.number_format($totalamount,3).'<br />';
	
	$taxrate = 0.10; // sales tax 10%
	$totalamount = $totalamount * (1 + $taxrate);
	echo 'Total including tax: $'.number_format($totalamount,2).'<br />';



	echo '<p>Your address is: </p>';
	echo $address.' <br />';
	
	$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
		@fp= fopen("$DOCUMENT_ROOT/../orders/orders.txt", "w");
	
	if (!$fp)
	
	{
		echo'<p><strong>Your order could not be process at this time. '
			.'Please try again later.</strong></p></body></html>';
			
			exit;
	}
	
	fwrite($fp, $outputstring);
	
	$outputstring = $date."\t".$tireqty." tires \t" .$oilqty." oil\t"
		.$sparkqty." spark plugs\t\$".$total
		."\t". $address."\n";


 ?>




Is This A Good Question/Topic? 0
  • +

Replies To: fopen()

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: fopen()

Posted 14 February 2009 - 09:33 PM

You cannot write data to a web server through fopen.
Was This Post Helpful? 0
  • +
  • -

#3 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Re: fopen()

Posted 14 February 2009 - 09:38 PM

View PostJackOfAllTrades, on 14 Feb, 2009 - 08:33 PM, said:

You cannot write data to a web server through fopen.


Well then I am completely misunderstanding what I am doing
Was This Post Helpful? 0
  • +
  • -

#4 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Re: fopen()

Posted 14 February 2009 - 09:59 PM

This is not set up to store data? why?
Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: fopen()

Posted 14 February 2009 - 10:15 PM

View PostJackOfAllTrades, on 14 Feb, 2009 - 10:33 PM, said:

You cannot write data to a web server through fopen.

Wow, well you're completely wrong.

Could your problem be that you're writing to the file before you put stuff in $outputstring?
Was This Post Helpful? 0
  • +
  • -

#6 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Re: fopen()

Posted 15 February 2009 - 01:59 AM

View Postsnoj, on 14 Feb, 2009 - 09:15 PM, said:

View PostJackOfAllTrades, on 14 Feb, 2009 - 10:33 PM, said:

You cannot write data to a web server through fopen.

Wow, well you're completely wrong.

Could your problem be that you're writing to the file before you put stuff in $outputstring?



I am actually not sure. is that the problem? as you can tell by my code I am about 24 hours into php so I am what they call a "rookie"
Was This Post Helpful? 0
  • +
  • -

#7 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: fopen()

Posted 15 February 2009 - 04:15 AM

Both Snoj and JackOfAllTrades are correct.

When you open a file handle with file_open on a http server, it connects to that file and sends http requests to it. Is does not open the file and store the contents in it, as a local file handle does.

The http server recieves your request and dispatches it to the file called. If it is a (php) script written to recieve data, it will store the data as you have scripted. If you have not put a script on the server, it will just call the file and generate a response (a file not found, or the contents of the file).

The code itself is not the problem, that's correct if this is what is supposed to happen. If you want to store a file on a server, you have to create a upload.php which stores your requests, or use another protocol like ftp.

EDIT:

If DOCUMENT_ROOT is a local dir, this should work as it is a local file handle. If you get an error, please post the exact contents of that error, then we can help troubleshoot.

Edit2:

YOu're using a @ in front of fopen(..). This will suppress any errors that occur during the opening of the file. For debugging purposes, remove the @ so you can see whether the fopen() call succeeds.

This post has been edited by Hary: 15 February 2009 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: fopen()

Posted 15 February 2009 - 06:45 AM

View Postsnoj, on 15 Feb, 2009 - 12:15 AM, said:

View PostJackOfAllTrades, on 14 Feb, 2009 - 10:33 PM, said:

You cannot write data to a web server through fopen.

Wow, well you're completely wrong.


I will agree I am completely wrong in this case, as I misread the fopen call as attempting to open a file on a web server.
Was This Post Helpful? 0
  • +
  • -

#9 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Re: fopen()

Posted 15 February 2009 - 03:19 PM

ok I got the script to do what I wanted it to do. Once a user selects the amount of each product they order and hit submit, they get a reciept in a php file. The data they enter is then sent to orders.txt file.

I am still have a problem $DOCUMENT_ROOT command

I had to type C:/Program_Files/wamp/www/web/orders/orders.txt in the fopen tag to send the data to the txt file. also the date does not appear in the txt file.

<?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>Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<?php

 
 
	
	echo '<p>Order processed at ';
	echo date('H:i, jS F'); 
	echo '</p>';
	

	
	
	echo '<p>Your order is as follows: </p>';
	echo $tireqty.' tires<br />';
	echo $oilqty.' bottles of oil <br />';
	echo $sparkqty.' Spark Plugs <br />';
	
	$totalqty =0;
	$totalqty= $tireqty + $oilqty +$sparkqty;
	echo 'Items ordered: '.$totalqty.'<br />'; //total number of items ordered
	
	
	$totalamount = 0.00; //total amount before tax
	
	
	define('TIREPRICE', 100);
	define('OILPRICE', 10);
	define('SPARKPRICE', 4);
	
	$totalamount = $tireqty * TIREPRICE
				+ $oilqty * OILPRICE
				+ $sparkqty * SPARKPRICE;
				
	echo 'Subtotal: $'.number_format($totalamount,3).'<br />';
	
	$taxrate = 0.10; // sales tax 10%
	$totalamount = $totalamount * (1 + $taxrate);
	echo 'Total including tax: $'.number_format($totalamount,2).'<br />';

   echo '<p>Your address is: </p>';
	echo $address.' <br />';

	
	$fp= fopen("C:/Program_Files/wamp/www/web/orders/orders.txt", 'ab');

	if (!$fp)
{
	echo '<p><strong> Your order could not be processed at this time. '
	.'Please try again later.</strong></p>';
	exit;
}
	$outputstring = $date." \t" .$tireqty." tires \t".$oilqty." oil\t"
		.$sparkqty." spark plugs\t\$".$totalamount
		."\t". $address."\n";
		
	fwrite($fp, $outputstring);
	
	fclose($fp);
?>

</body></html>


This post has been edited by chrisp200: 15 February 2009 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1