cannot open file...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

cannot open file...

Post by mabufo »

I have to send a string of text ($outputstring) to my file orders.txt. Of course, I can't open the file. I am supposed to send values that I have recieved from an order form($outputstring) into the text file(orders.txt).

Here is the part of the code:

Code: Select all

//attempts to open, and then write to a file.
		$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

		@ $fp = fopen("$DOCUMENT_ROOT/learnphp/orders.txt", 'a');
			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, strlen($outputstring));
					fclose($fp);	
				
/* there is more code than this - but this is specifically the problem area. */
Is there a logic flaw? Is the path to the file incorrect? It should referr to this path (http://mabufo.phpnet.us/learnphp/orders.txt) - correct? Or am I trying to declare the path to the nonexistant file incorrectly? (my use of 'a' in the fopen() function should create the file IIRC)

Any help is apprechiated - I'm stumped.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try echoing out the file name and path you are sending to fopen to see what it is. The DOCUMENT_ROOT var of the $_SERVER superglobal may not be being set properly (or at all).
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post by mabufo »

Yeah, the $DOCUMENT_ROOT variable returns this:

Code: Select all

/var/www/
Which is obviously not what I need. So do I enter the entire web address that leads to orders.txt? (http://mabufo.phpnet.us/learnphp/orders.txt)?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, but you can use a relative path ('./') or you can use dirname($file) . $file, or any of a host of other dirname finders.
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post by mabufo »

Ok, so if I understand how to use the dirname() function... it should be like this:

Code: Select all

$path = "learnphp/orders.txt";
		$filepath = dirname($path);
		echo "$filepath";
$filepath then returns this:

Code: Select all

learnphp/var/www/
I am starting to think that this problem may be a little above my head - the problem could possibly rest upon the server I'm on and not my code... is that possible? If so, I don't know what to do. However, I did try something. instead of adding just $filename to the first argument passed to fopen() - I added this:

Code: Select all

@ $fp = fopen("$filepath"."orders.txt", 'a');
Which should result in this string: learnphp/var/www/orders.txt

With that entered, the error message dissapeared (which makes me think that the file has been opened!) ... but my $outputsrting was not added to orders.txt. This leaves me completely stumped. So what I'm asking for now - are not little hints, but full on 'you do it for me' help. The book I am following with this, used $_SERVER['DOCUMENT_ROOT']. It would seem this has become way overcomplicated.

EDIT: For completeness - I will post the code of processorder.php, which is the page that I am working on atm. This page, takes input from a form and builds upon it - but again, the part that isn't working would be me trying to write the order to a file.

Code: Select all

<html>
<head>
<title>Order Processing.</title>
</head>
<body>

<?php 

	$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
	

	/* This scripts interprets, and builds upon,
	the information entered into the form that is
	found on the index.php page. */	


	//changes medium variable names to small names.
	$tireqty = $_POST['tireqty'];
	$oilqty = $_POST['oilqty'];
	$sparkqty = $_POST['sparkqty'];
	$address = $_POST['address'];
	$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
	$date = date('H:i, jS F');

	//tallies up the total amount of items
	$totalqty = 0;
	$totalqty = $tireqty + $oilqty + $sparkqty;
	
	
	if ($totalqty == 0)
	{
		echo 'You did not order any items! <br />';
	}
	else
	{

		echo 'Items ordered: '.$totalqty.' <br />';
		echo 'You have ordered: <br />';

		//outputs form values
		echo "$tireqty Tires <br />";
		echo "$oilqty Oil Cans <br />";
		echo "$sparkqty Spark Plugs <br />";

	
		$totalamount = 0.00;

		//needed constants
		define('TIREPRICE', 100);
		define('OILPRICE', 10);
		define('SPARKPRICE', 4);

	
		//tallies up total cost
		$totalamount = $tireqty * TIREPRICE
			+ $oilqty * OILPRICE
			+ $sparkqty * SPARKPRICE;


	

		/* this statement outputs the cost, without tax
		   The number_format() function is a php math 
		   command that sets decimal places on the number
		   passed to the function                         */
		echo 'Subtotal: $'.number_format($totalamount,2).'<br />';


		//calculates the cost - with tax.
		$taxrate = 0.09;
		$totalamount = $totalamount * (1 + $taxrate);
		echo 'Total including tax: $'.number_format($totalamount,2).'<br />';

		echo 'Shipping Address: '.$address.'<br />';


		
		//attempts to open, and then write to a file.

		$path = "learnphp/orders.txt";
		$filepath = dirname($path);
		
		
		
		@ $fp = fopen("$filepath"."orders.txt", 'a');
			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, strlen($outputstring));
					fclose($fp);	
				echo '<p>Order Written to file</p>';
	}

?>


</body>
</html>
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post by mabufo »

Solved. It turns out the problem was much simpler than I thought.

Code: Select all

@ $fp = fopen("orders.txt", 'a');
I think I need a nap.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Congrats.
Post Reply