baseDir = rtrim( $baseDir, '/' ) . '/'; } public function make_dir( $path, $zeroTime = FALSE ){ return $this->create_node( $path, NULL, $zeroTime ); } public function put_file( $path, $data, $zeroTime = FALSE ){ return $this->create_node( $path, $data, $zeroTime ); } private function create_node( $path, $data = NULL, $zeroTime = FALSE ){ $this->lastError = ''; $path = ( $path[0] == '/' ? '' : $this->baseDir ) . $path; if ( file_exists( $path ) ){ $this->lastError = 'File already exists'; return FALSE; } // Automatically set zeroTime option if parent directory has zero date $dir = dirname( $path ); if ( ! $zeroTime && file_exists( $dir ) ){ $zeroTime = filemtime( $dir ) < 86400; } // If zeroTime option is not set, use traditional file system functions if ( ! $zeroTime ){ if ( is_null( $data ) ){ if ( ! mkdir( $path, 0755, TRUE ) ){ $this->lastError = "Could not create directory {$path}"; return FALSE; } } else { $dir = dirname( $path ); if ( ! file_exists( $dir ) && ! mkdir( $dir, 0755, TRUE ) ){ $this->lastError = "Could not create directory {$dir}"; return FALSE; } if ( ! file_put_contents( $path, $data ) ){ $this->lastError = "Could not write file {$path}"; return FALSE; } } return TRUE; } // Otherwise create node and set its modification date to zero (i.e. 1970-01-01 00:00:00) // Zero dates of parent directories must be preserved, so find first path item with zero date ... $startPath = ''; foreach ( explode( '/', ltrim( $path, '/' ) ) as $item ){ $startPath .= "/{$item}"; if ( ! file_exists( $startPath ) || filemtime( $startPath ) < 86400 ){ break; } } if ( is_null( $data ) ){ if ( ! mkdir( $path, 0755, TRUE ) ){ $this->lastError = "Could not create directory {$path}"; return FALSE; } } else { if ( ! file_exists( dirname( $path ) ) && ! $this->create_node( dirname( $path ), NULL, TRUE ) ){ return FALSE; } if ( ! file_put_contents( $path, $data ) ){ $this->lastError = "Could not write file {$path}"; return FALSE; } chmod( $path, 0644 ); } do { touch( $path, 0 ); } while ( $path != $startPath && $path = dirname( $path ) ); return TRUE; } } // end of file fs_writer.class.php