8 Replies - 1840 Views - Last Post: 17 March 2013 - 05:15 PM Rate Topic: -----

#1 Dankwansere  Icon User is offline

  • D.I.C Head

Reputation: 64
  • View blog
  • Posts: 197
  • Joined: 09-November 09

Creating a file in php

Posted 16 March 2013 - 08:20 PM

Hey guys i'm fairly new to PHP. I just began to learn it like two weeks ago. I have alot of programming background so, so far everything is going well. I'm just stuck with this problem. I've created a basic ordering html file with a php script. when the User clicks the submit button the php script should run and create an order file in my directory. But for some reason the file does not get created. Any help will be greatly appreciated thank you. Here's the code

orderform.html

<html>
<head>
  <title>Bob's Auto Parts</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Form</h2>

<form action="processorder.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
  <td width="150">Item</td>
  <td width="15">Quantity</td>
</tr>
<tr>
  <td>Tires</td>
  <td align="left"><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
  <td>Oil</td>
  <td align="left"><input type="text" name="oilqty" size="3" maxlength="3"/></td>
</tr>
<tr>
  <td>Spark Plugs</td>
  <td align="left"><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
  <td>Shipping Address</td>
  <td align="center"><input type="text" name="address" size="40" maxlength="40"/></td>
</tr>
<tr>
  <td colspan="2" align="center"><input type="submit" value="Submit Order"/></td>
</tr>
</table>
</form>

</body>
</html>



processorder.php

<?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 /../home/dankwansere/Desktop/orders.txt", 'ab');


	echo $fp;

	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>






Is This A Good Question/Topic? 0
  • +

Replies To: Creating a file in php

#2 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Creating a file in php

Posted 16 March 2013 - 09:54 PM

error_reporting(E_ALL);
ini_set('display_errors', '1');


will force php to output errors on the page for you, definitely recommended during testing

beyond that, your line that says..
"$DOCUMENT_ROOT /../home/dankwansere/Desktop/orders.txt"


You'll need to use $docRoot . "filepath" .. in php the . (period) is the concat operator. also, double check this line as it looks like you've just got two things running together here. and finally, make sure your file has write permissions and you should be good (rough read over the code, you may find other issues)

This post has been edited by Takk: 16 March 2013 - 09:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Creating a file in php

Posted 16 March 2013 - 11:53 PM

You should also remove the '@' symbol. It's slow and should rarely ever be used. By the way, you don't need to go with the complicated fopen/fwrite/fclose way, you can just use file_put_contents and it'll do all of that for you.

Takk that isn't neccessary. Double quotes are parsed for variables in PHP, so if $DOCUMENT_ROOT was something like "/hello/mysite/", the output would be "/hello/mysite/ /../home/dnakwansere/Desktop/orders.txt".
Was This Post Helpful? 3
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3197
  • View blog
  • Posts: 10,713
  • Joined: 12-December 12

Re: Creating a file in php

Posted 17 March 2013 - 03:06 AM

"$DOCUMENT_ROOT /../home/dankwansere/Desktop/orders.txt"


this is incorrect, particularly the space. It could be:

"$DOCUMENT_ROOT/home/dankwansere/Desktop/orders.txt"
// or
"../home/dankwansere/Desktop/orders.txt"

// which is one level up from the current location.

It would easiest to test with just "orders.txt" to make sure it is saved somewhere, then modify the path.
Was This Post Helpful? 4
  • +
  • -

#5 CynicalSolution  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 15
  • Joined: 17-March 13

Re: Creating a file in php

Posted 17 March 2013 - 06:20 AM

Look up concatenation in PHP
Was This Post Helpful? -2
  • +
  • -

#6 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Creating a file in php

Posted 17 March 2013 - 10:02 AM

Good call you two
Was This Post Helpful? 0
  • +
  • -

#7 Dankwansere  Icon User is offline

  • D.I.C Head

Reputation: 64
  • View blog
  • Posts: 197
  • Joined: 09-November 09

Re: Creating a file in php

Posted 17 March 2013 - 03:57 PM

The book that i'm using recommended that I use the "@" symbol in front of open. it says it is used to suppress any errors resulting from the function call.

I guess if its not really needed then I will omit it from my future codes
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Creating a file in php

Posted 17 March 2013 - 04:51 PM

Think about that for a second, though. Why on earth would you want to suppress the error messages resulting from the failed function call? Presumably you'd want to fix the error that caused the failure. The error messages are usually the most significant clues as to what is going wrong. Without them you are debugging blind.

If the goal here is to hide the errors from the users, then PHP has other methods for doing that. It's quite capable of logging errors to a file instead of displaying them. That is, in fact, how production servers should be (and usually are) configured.
Was This Post Helpful? 0
  • +
  • -

#9 Dankwansere  Icon User is offline

  • D.I.C Head

Reputation: 64
  • View blog
  • Posts: 197
  • Joined: 09-November 09

Re: Creating a file in php

Posted 17 March 2013 - 05:15 PM

oh Okay thnx!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1