Path of the needed file * * @return created If the file has been created successfully * */ public static function create($file){ /* (0) Checks arguments */ if( !is_string($file) || is_dir($file) ) return false; /* (1) Creates file */ try{ fclose( fopen($file, 'w') ); return true; }catch(Exception $e){ return false; } } /* READS FILE'S CONTENT * * @file File to read * */ public static function read($file){ /* (0) Checks arguments */ if( !is_string($file) ) throw new \Error('Wrong argument for read().'); /* (1) Initializing driver on file (read-flag) */ $driver = new \SplFileObject($file, 'r'); /* (2) Read lines */ $read = ''; $line = 0; while( $driver->current() ){ $read .= $driver->current(); $driver->next(); } /* (3) Returns result */ return $read; } /* WRITES CONTENT TO A FILE * * @file File to write to * @content Content to write * * @return written Returns if the content have been written successfully * * @note: Creates file if it is possible with `file_put_contents()` * */ public static function write($file, $content){ /* (0) Checks arguments */ if( !is_string($file) || !is_string($content) ) return false; /* (1) Erase file */ try{ fclose( fopen($file, 'w') ); }catch(Exception $e){ return false; } /* (2) Get driver (write-flag) */ $driver = new \SplFileObject($file, 'r+'); /* (3) Writes content */ return !is_null( $driver->fwrite($content) ); } /* APPENDS CONTENT TO A FILE * * @file File to append content to * @content Content to append * * @return append Returns if the content have been append successfully * * @note: If file doesn't exists, returns false * */ public static function append($file, $content){ /* (0) Checks arguments */ if( !is_file($file) || !is_string($content) ) return false; /* (1) Get driver (append-flag) */ $driver = new \SplFileObject($file, 'a'); /* (2) append content */ return !is_null( $driver->fwrite($content.PHP_EOL) ); } } ?>