Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class flockTest{
- public $file;
- public $fp;
- public $text;
- function __construct($file){
- $this->file=$file;
- $this->fp=fopen($this->file, "w+");
- $this->text='Here is the text.';
- }
- function flock_test(){
- if(flock($this->fp, LOCK_EX | LOCK_NB)){
- echo "Locked the file.<br/>";
- if(is_writable($this->file)){
- if(fwrite($his->fp, $this->text)==FALSE){
- throw new Exception("Can't write $this->file<br />");
- }
- else{
- echo "Created $this->file<br />";
- }
- }
- else{
- echo 'Write problem';
- }
- flock($this->fp, LOCK_UN);
- echo "Unlock the file.";
- }
- else{
- echo "Couldn't lock the file.";
- }
- fclose($this->fp);
- }
- }
- $testLock=new flockTest('lock.txt');
- try{
- $testLock->flock_test();
- }
- catch(Exception $e){
- echo $e->getMessage();
- }
- ?>
Add Comment
Please, Sign In to add comment