Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class File
  2. {
  3.  
  4. public static function read($path, $default = NULL)
  5. {
  6. $fp = @fopen($path, 'r');
  7. if( ! $fp)
  8. return $default;
  9.  
  10. flock($fp, LOCK_SH);
  11. $data = fread($fp, filesize($path));
  12. flock($fp, LOCK_UN);
  13. fclose($fp);
  14.  
  15. return $data;
  16. }
  17.  
  18.  
  19. public static function write($path, $data)
  20. {
  21. self::check(dirname($path));
  22.  
  23. $fp = fopen($path, 'c');
  24. flock($fp, LOCK_EX);
  25. ftruncate($fp, 0);
  26. fwrite($fp, $data);
  27. fflush($fp);
  28. flock($fp, LOCK_UN);
  29. fclose($fp);
  30.  
  31. return $data;
  32. }
  33.  
  34.  
  35.  
  36. public static function check($dir)
  37. {
  38. if( ! is_dir($dir))
  39. {
  40. // https://en.wikipedia.org/wiki/Chmod#System_call
  41. @mkdir($dir, 06740, true);
  42. @chmod($dir, 06740);
  43. }
  44. return $dir;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement