Home Tutorial Php Phpfiles PHP flock() Function and Example

 
 

Share on Google+Share on Google+
PHP flock() Function and Example
Posted on: October 29, 2009 at 12:00 AM
Advertisement
In this part of the tutorial we will learn about the PHP flock() function in context of file handling. And also we will see the example related to the flock function

Syntax
bool flock ( file_name ,lock [,block] )

  • It puts the locks on the file or remove the lock
  • It is used for the  simple reader/writer file  model

lock values are

  • LOCK_SH - It is Shared lock  for reader. Other processes can access  the file.
  • LOCK_EX - It is Exclusive lock for writer. It doesn't allow  other processes to access the file
  • LOCK_UN -It releases a shared or exclusive lock
  • LOCK_NB - It doesn't  block other processes when the file has lock


Example of PHP Flock() Function

Code for flock() function PHP

<?php
$file
= fopen("c:/rose1/ram.txt","w+");
if (flock($file,LOCK_SH))
 fwrite($file,"India is a great country");
else
echo
"locking  problem in file";
?>

Output
ram.txt is not cotaining any thing as LOCK_SH makes the lock for reader and doesn't allow for writing

Code

<?php
$file = fopen("c:/rose1/ram.txt","w+");
if
(flock($file,LOCK_EX))
 fwrite(
$file,"India is a great country");
else
echo
"locking  problem in file";
?>

Output

now ram.txt has India is a great country

Advertisement

Related Tags for PHP flock() Function and Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 29, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com