Guest User

Untitled

a guest
Jan 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. <?php
  2. class flockTest{
  3. public $file;
  4. public $fp;
  5. public $text;
  6. function __construct($file){
  7. $this->file=$file;
  8. $this->fp=fopen($this->file, "w+");
  9. $this->text='Here is the text.';
  10. }
  11. function flock_test(){
  12. if(flock($this->fp, LOCK_EX | LOCK_NB)){
  13. echo "Locked the file.<br/>";
  14. if(is_writable($this->file)){
  15. if(fwrite($his->fp, $this->text)==FALSE){
  16. throw new Exception("Can't write $this->file<br />");
  17. }
  18. else{
  19. echo "Created $this->file<br />";
  20. }
  21. }
  22. else{
  23. echo 'Write problem';
  24. }
  25. flock($this->fp, LOCK_UN);
  26. echo "Unlock the file.";
  27. }
  28. else{
  29. echo "Couldn't lock the file.";
  30. }
  31. fclose($this->fp);
  32. }
  33. }
  34. $testLock=new flockTest('lock.txt');
  35. try{
  36. $testLock->flock_test();
  37. }
  38. catch(Exception $e){
  39. echo $e->getMessage();
  40. }
  41. ?>
Add Comment
Please, Sign In to add comment