2016-10-25 22:52:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace filemanager\core;
|
|
|
|
|
|
|
|
|
|
|
|
class FileManager{
|
|
|
|
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
/* CREATES A FILE WITH ITS NEEDED DIRECTORIES
|
|
|
|
*
|
|
|
|
* @path<String> Path of the needed file
|
|
|
|
*
|
|
|
|
* @return created<Boolean> 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;
|
2016-10-27 16:34:28 +00:00
|
|
|
}catch(\Exception $e){ return false; }
|
2016-10-26 15:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
/* READS FILE'S CONTENT
|
|
|
|
*
|
|
|
|
* @file<String> File to read
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function read($file){
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (0) Checks arguments */
|
|
|
|
if( !is_string($file) )
|
2016-10-27 16:34:28 +00:00
|
|
|
throw new \Exception('Wrong argument for read(<String>).');
|
2016-10-25 22:52:28 +00:00
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (1) Initializing driver on file (read-flag) */
|
|
|
|
$driver = new \SplFileObject($file, 'r');
|
2016-10-25 22:52:28 +00:00
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (2) Read lines */
|
2016-10-25 22:52:28 +00:00
|
|
|
$read = '';
|
|
|
|
|
|
|
|
$line = 0;
|
|
|
|
while( $driver->current() ){
|
2016-10-26 15:15:00 +00:00
|
|
|
$read .= $driver->current();
|
2016-10-25 22:52:28 +00:00
|
|
|
$driver->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Returns result */
|
|
|
|
return $read;
|
|
|
|
}
|
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
/* READS A FILE'S SPECIFIC LINE
|
|
|
|
*
|
|
|
|
* @file<String> File to read
|
|
|
|
* @line<int> Line to read
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function readline($file, $line){
|
|
|
|
/* (0) Checks arguments */
|
|
|
|
if( !is_string($file) || intval($line) !== $line )
|
|
|
|
throw new \Exception('Wrong argument for readline(<String>, <int>).');
|
|
|
|
|
|
|
|
/* (1) Initializing driver on file (read-flag) */
|
|
|
|
$driver = new \SplFileObject($file, 'r');
|
|
|
|
|
|
|
|
/* (2) Goto specific line */
|
|
|
|
$driver->seek($line);
|
|
|
|
|
|
|
|
/* (3) Return line's content */
|
|
|
|
if( $driver->key() == $line )
|
|
|
|
return $driver->current();
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WRITES A FILE'S SPECIFIC LINE
|
|
|
|
*
|
|
|
|
* @file<String> File to read
|
|
|
|
* @line<int> Line to read
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function writeline($file, $line){
|
|
|
|
/* (0) Checks arguments */
|
|
|
|
if( !is_string($file) || intval($line) !== $line )
|
|
|
|
throw new \Exception('Wrong argument for writeline(<String>, <int>).');
|
|
|
|
|
|
|
|
/* (1) Initializing driver on file (read-flag) */
|
|
|
|
$driver = new \SplFileObject($file, 'r');
|
|
|
|
|
|
|
|
/* (2) Goto specific line */
|
|
|
|
$driver->seek($line);
|
|
|
|
|
|
|
|
/* (3) Return line's content */
|
|
|
|
if( $driver->key() == $line )
|
|
|
|
return $driver->current();
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
/* WRITES CONTENT TO A FILE
|
|
|
|
*
|
|
|
|
* @file<String> File to write to
|
|
|
|
* @content<String> Content to write
|
|
|
|
*
|
2016-10-26 15:15:00 +00:00
|
|
|
* @return written<Boolean> Returns if the content have been written successfully
|
|
|
|
*
|
|
|
|
* @note: Creates file if it is possible with `file_put_contents()`
|
|
|
|
*
|
2016-10-25 22:52:28 +00:00
|
|
|
*/
|
|
|
|
public static function write($file, $content){
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (0) Checks arguments */
|
|
|
|
if( !is_string($file) || !is_string($content) )
|
|
|
|
return false;
|
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
/* (1) Erase file */
|
2016-10-26 15:15:00 +00:00
|
|
|
try{
|
|
|
|
fclose( fopen($file, 'w') );
|
2016-10-27 16:34:28 +00:00
|
|
|
}catch(\Exception $e){ return false; }
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
/* (2) Get driver (write-flag) */
|
|
|
|
$driver = new \SplFileObject($file, 'r+');
|
|
|
|
|
|
|
|
/* (3) Writes content */
|
2016-10-26 15:15:00 +00:00
|
|
|
return !is_null( $driver->fwrite($content) );
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* APPENDS CONTENT TO A FILE
|
|
|
|
*
|
|
|
|
* @file<String> File to append content to
|
|
|
|
* @content<String> Content to append
|
|
|
|
*
|
2016-10-26 15:15:00 +00:00
|
|
|
* @return append<Boolean> Returns if the content have been append successfully
|
|
|
|
*
|
|
|
|
* @note: If file doesn't exists, returns false
|
|
|
|
*
|
2016-10-25 22:52:28 +00:00
|
|
|
*/
|
|
|
|
public static function append($file, $content){
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (0) Checks arguments */
|
|
|
|
if( !is_file($file) || !is_string($content) )
|
|
|
|
return false;
|
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
/* (1) Get driver (append-flag) */
|
|
|
|
$driver = new \SplFileObject($file, 'a');
|
|
|
|
|
|
|
|
/* (2) append content */
|
2016-10-26 15:15:00 +00:00
|
|
|
return !is_null( $driver->fwrite($content.PHP_EOL) );
|
2016-10-25 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|