"Flock" function is not working

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
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

"Flock" function is not working

Post by lawrence_chtan »

Hi to all the Guru

I notice when i use the function Flock in php5.1 I was told that i had an error

error is shown as

"Warning: flock() expects parameter 1 to be resource, boolean given in c:\Inetpub\wwwroot\02\processorder.php on line 69"


where by if i use the EasyPHP 1.7 at another virtual Machine. the Flock function is working.

Have i done anything wrong for that?

Thanks in advance for your guidance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post your code.
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

<?php
  // create short variable names
  $tireqty = $_POST['tireqty'];   // value derived from last page 10
  $oilqty = $_POST['oilqty'];       // value derived from last page 20 
  $sparkqty = $_POST['sparkqty'];       // value derived from last page 30
  $address = $_POST['address'];         // Singapore
  $date = date('H:i, jS F');
?>

<html>
<body>
<?php
$TotalAmount=$tireqty+$oilqty+$sparkqty;        //$TotalAmount=60
echo '<p>Total of order is '.$TotalAmount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';      // Singapore

$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/../orders/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>
it is working at my WM with Easyphp1.7. not in my VM with Php 5.1

hm...


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove the @ in front of fopen(). I'm going to bet register_globals are off, which they should be. $_SERVER['DOCUMENT_ROOT'] should be used instead of $DOCUMENT_ROOT
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

yes the register_global is off

but i dun think that is the solution. because i am very sure the coding is working under the Easyphp compiler.

I start to worry whether is my installation process is wrong.

1. Create PHP
2. Set php.ini
3. setting under IIS
4. pear installed

but i still dun get the fopen working. same go to Mail() and ODBC_exec.

I had tried to removed the @ sign. and it still dun work... instead i get another line of error as well. :(

may i know. do we need to copy some other DLL in order to make the PHP page working beside the standard Ext from php 5.1 zip file?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no you don't. $DOCUMENT_ROOT will not exist if you don't have register_globals on. You should not have them on. EasyPHP likely has them on. They are defaulted to on in older versions of PHP and some installers set it to on for various (albeit bad) reasons.

Post the errors you are getting now.
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

I notice something

in my VM with PHP 5.1

I use the

Code: Select all

<?php
  echo 'Function sets supported in this install are:<br />';
  $extensions = get_loaded_extensions();
  foreach ($extensions as $each_ext)
  {
    echo "$each_ext <br />";
    echo '<ul>';
    $ext_funcs = get_extension_funcs($each_ext);
    foreach($ext_funcs as $func)
    {
       echo "<li> $func </li>";
    } 
    echo '</ul>';
  }
?>
I dun have the Flock function

where If i use the VM with Easy PHP 1.7

I have the Flock Function

i think it is the extension issue. ... same go to the ODBC_Exec function
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

flock() is not apart of any extension. It's a core function.

Check your php.ini settings. Make sure flock() isn't in the disabled functions list. If it's not, which is likely, did you build PHP yourself on your virtual machine? If so, what configuration options did you specify?
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

Hi Fyed

Yes I install from scratch. which i bought a book. that taught me to

1. Copy All the DLLs. to C:\windows\
2. Copy php.ini to C:\Windows\
3. amend the C:\php.ini
- Find Extension_dir = C:/php/ext
- doc_root = "c:/inetpub/wwwroot"
- remove the ";" from "extension = php_pdf.dll"
- remove the ";" from "extension = php_gd2.dll, php_imap.dll and php_mysqli.dll"
4. go to my IIS ..
- add in .php in to the home directory
- add php at ISAPI filter.


how do i know which dll are need to be remove the ";". Is there any reference to check

so sorry for troubling u.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Change this line:

Code: Select all

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

Code: Select all

$fp = fopen(realpath($_SERVER['DOCUMENT_ROOT'] . '/../orders/orders.txt'), 'ab');
and see if you receive a different error.
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

same problem

Warning
: flock() expects parameter 1 to be resource, boolean given in c:\Inetpub\wwwroot\02\processorder.php

even i had pre-created the folder

:(

feel so stupid now :cry:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

run this:

Code: Select all

<?php

var_dump(realpath($_SERVER['DOCUMENT_ROOT'] . '/../orders/orders.txt'));

?>
and post the output :)
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Also always remember flock() in PHP Windows ISAPI will not inherit the system permissions so LOCK_EX and LOCK_SH will just hang your script instead of returning FALSE if a another instance is using that file or folder. It works as it it should only in CGI PHP under windows! If using ISAPI PHP under windows, then create a fake lock to test if a file or folder is in use!


yj!
lawrence_chtan
Forum Newbie
Posts: 22
Joined: Mon Jan 02, 2006 8:20 pm
Location: SINGAPORE

Post by lawrence_chtan »

hi

i had solved this at last.

reason is the \ and the / issue...

thanks for all the advice from all of you..

now i am one step further to understand php. :P

Thanks
Post Reply