Page 1 of 1

Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 1:04 am
by jdphp1
This should be an easy one but as I just started I haven't a clue as to what's going on. ready to learn though.


using the PHP and MySQL web development book and going through the examples in the 2nd chap -

I think the file it is supposed to write to does not exist and or it cannot create the file due to the path given and not sure how to correct it.
As I am learning I would like to get these little things out of the way to be a good programmer - thanks in advance for any help.


get this error

Warning: flock(): supplied argument is not a valid stream resource in /home/e/f/a/14751/14751/public_html/php/processorder.php on line 72

Code: Select all

<?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/../php/orders.txt", 'ab');
[b]
	flock($fp, LOCK_EX);[/b]

	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>

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 8:50 am
by mkz
First, a tip: It's easier to read code when you post it in PHP code tags. See the PHP code button right above the post text box; that's what it's for.

The error is because that path is invalid. Strip away the first part of the path and try opening just "orders.txt". It should create the file in the same directory as the script that's running.

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 1:09 pm
by jdphp1
I am uploading to a web server that is a linux box but still get the same error.

Do I need to change the php.ini file so I can write properly?

Is it the path that I need to change or use the FTP or HTTP protocol ?

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 1:17 pm
by Monotoko
This here seems to be your problem:

Code: Select all

<span style="font-weight: bold">
        flock($fp, LOCK_EX);</span>
Where's the span come from? Your still in PHP!

If that isn't it you can also do some debugging yourself by changing it to the following:

Code: Select all

<?php
$err = flock($fp, LOCK_EX);
echo $err;
?>
It will echo 1 when it works :)

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 1:57 pm
by jdphp1
It did not work - this is frustrating but I need to understand how to create and write a file on a remote server.

Also I never put that <span> tag in there - the first responder placed it there.

The issue is here - the php folder is where I want to place the newly created document - does it make difference
that the php scripts are placed in the same folder I am attempting to create and write to?

this is the path to the folder /14751/public_html/php


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

flock($fp, LOCK_EX);

Code: Select all

<?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/../php/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>

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 2:20 pm
by jason
Remove the @ from this line

Code: Select all

@$fp = fopen("$DOCUMENT_ROOT/../php/orders.txt", 'ab');
@ in front blocks the line from throwing any errors, and we want to see the specific error that pops up.

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 3:28 pm
by jdphp1
Warning: fopen(/home/e/f/a/14751/14751/public_html/../php/orders.txt): failed to open stream: No such file or directory in /home/e/f/a/14751/14751/public_html/php/processorder.php on line 70

Warning: flock(): supplied argument is not a valid stream resource in /home/e/f/a/14751/14751/public_html/php/processorder.php on line 72

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 3:34 pm
by jason
So... does that directory exist already? If not, you'll have to create the php directory and make it writable before you'll be able to create the file with fopen.

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 3:54 pm
by jdphp1
the php(folder) directory exists - all the scripts that I have written are located there.
http://www.allapparel.biz/php/orderform.html

I was under the impression that I had to change the protocol in the line to this
$fp = fopen("http://www.allapparel.biz/../php/orders.txt", 'ab');

or I have to change a setting in the php.ini file which would allow the file to be created, writeable and readable.

To me this should not be this difficult and have been seeking answers everywhere - I wish I were more programming literate...

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 6:02 pm
by jdphp1
I found the answer to the error issue - upgraded the php on my serve from 4 to 5 and uploaded my php.ini file
that seemed to make the script work.

However it's still not able to create or write to a file. What do I change in the ini file?

Still learning but thank you for all those who responded!!

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 6:12 pm
by mikosiko
Warning: fopen(/home/e/f/a/14751/14751/public_html/../php/orders.txt): failed to open stream: No such file or directory in /home/e/f/a/14751/14751/public_html/php/processorder.php on line 70
looking those 2 lines seems that you have an extra "../" in the first one, hence fopen() is failing

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 8:48 pm
by jason
jdphp1 wrote:the php(folder) directory exists - all the scripts that I have written are located there.
http://www.allapparel.biz/php/orderform.html
No. That's not the same.

Look, this is the error:

Code: Select all

/home/e/f/a/14751/14751/public_html/../php/orders.txt
You are dealing with files on the server, not a URL. A URL is not a direct representation of the file system. What that path is pointing to is in fact this:

Code: Select all

/home/e/f/a/14751/14751/php/orders.txt
The directory you have is in fact here:

/home/e/f/a/14751/14751/public_html/php/orders.txt

What you want to do, most likely, is display do:

Code: Select all

$fp = fopen('orders.txt');
Do you know what the .. means in the path? It means go up a directory, or back a directory. So, in the path, when you have /public_html/../php/orders.txt, you are in fact saying go into public_html, then, back out of public_html, then go into php, then create orders.txt.
I was under the impression that I had to change the protocol in the line to this
$fp = fopen("http://www.allapparel.biz/../php/orders.txt", 'ab');
No. No no no. You are changing things without understanding what they are doing.
or I have to change a setting in the php.ini file which would allow the file to be created, writeable and readable.
No no no. Read the error messages. They tell you everything you need to know.
To me this should not be this difficult and have been seeking answers everywhere - I wish I were more programming literate...
It's really not. Just focus on the error messages and understanding what they do. Don't change context (file system to URL for example), and make sure you understand what's going on before you start changing things around.

Re: Newbie on the board - PHP error

Posted: Wed Oct 06, 2010 8:50 pm
by jason
jdphp1 wrote:I found the answer to the error issue - upgraded the php on my serve from 4 to 5 and uploaded my php.ini file
that seemed to make the script work.

However it's still not able to create or write to a file. What do I change in the ini file?

Still learning but thank you for all those who responded!!
The "error issue" is what you need to solve the problems. php.ini is probably just have errors_display off. That's fine, but enable it for development. Please!

Basically, you are still getting the error. It's just not being displayed to you.