Subversion Repositories phpLibraryV2

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 rodolico 1
<?php
2
 
3
   /*
4
    * Wrapper for file functions. Ensures the path to the file is available
5
    * then opens the file (default append/binary mode) and grabs an 
6
    * exclusive lock
7
    */
8
   class File {
9
 
10
      // fully qualified path and file name
11
      protected $fullFileName;
12
      // file name
13
      protected $filename;
14
      // path to file
15
      protected $path;
16
      // permissions to assign to path
17
      protected $pathPermission = 0777;
18
      // mode to open in. Default is append in binary mode
19
      public $filemode = 'ab';
20
      // file handle
21
      protected $fh = null;
22
 
23
      /*
24
       * constructor must have file name, all else is default
25
       * however filename may be empty or null
26
       */
27
      public function __construct( $filename = null, $filemode = null ) {
28
         //if ( $filemode !== null ) $this->filemode = $filemode;
29
         if ( isset( $filename ) && $filename ) {
30
            $temp = pathinfo( $filename );
31
            $this->filename = $temp['basename'];
32
            $this->path = $temp['dirname'];
33
            $this->fullFileName = $this->path . '/' . $this->filename;
34
            $this->makePath(); // will return false if we could not create the path
35
         }
36
         $this->openFile();
37
      } // __construct
38
 
39
      public function openFile() {
40
         if ( ! isset( $fh ) && isset( $this->fullFileName ) ) {
41
            $this->fh = fopen( $this->fullFileName, $this->filemode );
42
            /*if ( $this->fh === false )
43
               die( "could not write to $this->fullFileName" );
44
            // get exclusive lock during script life.
45
            if ( ! flock( $this->fh, LOCK_EX ) )
46
               die( "could not acquire lock on $this->fullFileName" );
47
               */
48
         }
49
      }
50
 
51
      public function closeFile () {
52
         if ( isset( $this->fp) ) fclose( $this->fp );
53
      }
54
 
55
      /*
56
       * creates path $path if it does not exist
57
       * sets permissions to $pathPermission
58
       * Note: this does a recursive creation (mkdir -p) but does not
59
       *       check permissions on existing directories
60
       * returns true on success, false on failure
61
       */
62
      private function makePath() {
63
         /* we'll return true if the path exists. HOWEVER, note that
64
          * we do not check permissions, so this could cause issues
65
          */
66
         if ( is_dir( $this->path ) )
67
            return true; 
68
         /* path does not exist. Save the current umask and set it 
69
          * temporarily to 0, then create the directory and restore
70
          * umask
71
          */
72
         $oldmask = umask(0); // set umask to 0 temporarily
73
         $result = mkdir( $this->path, $this->pathPermission, true ); // recursively build tree
74
         umask( $oldmask ); // restore umask
75
         return $result;
76
      } // makePath
77
   } // class file
78
 
79
?>
80