FileManager class.
$filemanager->readFile($keys, $separator);
USERNAME : Foo
PASSWORD : Bar
EMAIL : foobar@email.com
TALL : true
$filemanager = new FileManager();
$filemanager->setUrl('directory'); // set base directory or path
getFolders method returns the directories in a predefined path:
$filemanager->setUrl('some_path'); // set base directory
$filemanager->getFolders(); // returns the available directories
getFiles method returns the files available in a predefined directory or path:
$filemanager->setUrl('some_path'); // set base directory
$filemanager->getFiles(); // returns the full path of files available file in directory
$filemanager->getFiles(false); // returns only names of files (and extension) in a directory
$filemanager->getFiles(false, false); // returns only names of files in a directory without the extension name
addDir method adds a new directory. This may return null if directory cannot be created
$filemanager->addDir('some_path'); // creates a new directory
lastDir method returns the last set directory.
This method is useful when handling zip files.
$filemanager->setUrl('myurl')
$filemanager->lastDir(); // myurl
$filemanager->openFile($strict, $url)
where:
$strict : boolean true permits to create a new directory for a url that does not exist.
$url : an optional new url or path to be created.
If not supplied, openFile uses the default url set.
If supplied, overides the default set url from setUrl()
$filemanager->setUrl('somepath');
if( $filemanager->openFile(true) ) {
//file path opened successfully
}
openFile() method by supplying
the url as the second argument as shown below
if( $filemanager->openFile(true, 'somefile.txt') ) {
//file path opened successfully
}
readFile method is used to read a readable file.
This method reads a file line by line to fetch supplied keys.
Using test file:
$filemanager->readFile($keys, $separator);
where:
$keys : array of keys to be read in given local file
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$filemanager->setUrl('test.txt');
$read = $filemanager->readFile(['USERNAME', 'PASSWORD']);
var_dump($read); // ['USERNAME' => 'Foo', 'PASSWORD' => 'Bar']
$check = $filemanager->readFile('USERNAME', true);
var_dump($check); // returns true because USERNAME key exists in testfile
readAll method is used to read an entire readable file.
This method reads a file line by line and stores each key with its respective values.
Using test file:
$filemanager->readAll($separator);
where:
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :.
$filemanager->setUrl('test.txt');
$read = $filemanager->readAll();
var_dump($read); // ['USERNAME' => 'Foo', 'PASSWORD' => 'Bar', 'EMAIL' => 'foobar@email.com', 'TALL'=>'true']
textWrite method write a text into a file using supplied
array key-value pairs
$filemanager->textWrite([$key => $value], $options);
where:
$key : line key
$value : value of supplied key
$options : array containing postions of where text should be written. [before, after]
$filemanager->setUrl('text.txt');
var_dump($filemanager->read('AGE')); //returns: false
$filemanager->textWrite(['AGE' => '30']); //write AGE to a new line
$filemanager->textWrite(['AGE' => '30'], ['after' => 'USERNAME']); //write AGE to a position after USERNAME, else write to a new line
$filemanager->textWrite(['AGE' => '30'], ['before' => 'USERNAME']); //write AGE to a position before USERNAME, else write to a new line
var_dump($filemanager->read('AGE')); //returns: ['AGE' => '30']
textLine method writes a new line into a writable file.
Example is shown below:
$filemanager->textLine($number, $options);
where:
$number : number of lines to be added
$options: array containing postions of where line should be written [before, after]
$filemanager->textLine(4, ['after'=>'USERNAME']); //add four new lines after USERNAME line
$filemanager->textReplace([$key => $value], $separator);
where:
$key : target key
$value : target key's new value.
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$filemanager->textReplace(['USERNAME'=>'NewFoo']); //replaces foo with NewFoo //Note:: This can be done for multiple keys.
$filemanager->textUpdate([$key => $value], $updates, $separator);
where:
$key : target key
$value : target key's new value
$updates : a referenced variable that contains all successful updates
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$filemanager->textUpdate(['USERNAME'=>'NewFoo'], $updates); //replaces foo with NewFoo var_dump($updates) // returns: ['USERNAME'] //Note:: This can be done for multiple keys.
$filemanager->textDelete($key, $dels, $separator);
where:
$key : target key
$updates : a referenced variable that contains all successful deletes
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$filemanager->textReplace(4, ['USERNAME'=>'NewFoo']); //replaces foo with NewFoo //Note:: This can be done for multiple keys.
load method reads an entire file without instantiating the
FileManager class similarly as readAll method. The instantiation is
done within itself.
FileManager::load($path, $separator);
where:
$path : path of file
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$contents = FileManager::load('test.txt', ':');
var_dump($contents); //returns the entire array key and value pairs.
load method reads an entire file just like the load
method. All data obtained are saved into the $_ENV global variable.
FileManager::loadenv($path, $separator);
where:
$path : path of file
$separator : An optional character separator used to separate keys and values.
By default, this is set as colon :. When set as true,
the readFile checks if the key supplied exists within the file and returns
a bool of true or false
$contents = FileManager::loadenv('test.txt', ':');
var_dump($contents); //returns the entire array key and value pairs.
$filemanager->copyTo($newpath, $newname, $strict);
where:
$newpath : newpath of file
$newname : An optional new file name
$strict : prevents previous errors from stopping operation when handling zip files.
$filemanager->setUrl('test.txt');
$filemanager->copyTo('newdirectory/files', 'newtext.txt', true);
$filemanager->copyTo($newpath, $newname, $strict);
where:
$newpath : newpath of file
$newname : An optional new file name
$strict : prevents previous errors from stopping operation when handling zip files.
$filemanager->setUrl('test.txt');
$filemanager->moveTo('newdirectory/files', 'newtext.txt', true);
$filemanager->move($path1, $path2);
where:
$path1 : old path or new destination of folder or file
$path2 : new destination of folder or file
$filemanager->move('somefile.txt', 'newdir/somefile.txt'); moves from one path to another
$filemanager->setUrl('test.txt');
$filemanager->move('newdir/test.txt'); moves from predefined path to another
$filemanager->zipUrl($output);
where:
$output : output name of zipfile
$filemanager->setUrl('test.txt'); // set a file path
$filemanager->zip('test.zip'); // zip file to a new name
$filemanager->move('newdir/test.zip'); // moves zipped file to a new location.
$filemanager->decompress($delete, $strict);
where:
$delete : a boolean of true deletes the compressed file after extraction
$strict : a boolean of true prevent previous errors from stopping code execution
$filemanager->setUrl('test.zip'); // set a file path
$filemanager->decompress(true); // decompress file to current location and delete compressed file after
$filemanager->response($message);
where:
$message : sets a new message when not supplied, response returns the last set response
$filemanager->setUrl('test.zip'); // set a file path
$filemanager->decompress(true); // decompress file to current location and delete compressed file after
var_dump($filemanager->response()); // returns last response set
$filemanager->response(message);
$filemanager->setUrl('test.zip'); // set a file path
$filemanager->decompress(true); // decompress file to current location and delete compressed file after
var_dump($filemanager->err()); // returns last error set
$filemanager->succeeds();
$filemanager->setUrl('test.zip'); // set a file path
$filemanager->decompress(true); // decompress file to current location and delete compressed file after
if($filemanager->succeeds()) {
//file decompressed successfully
}
$filemanager->fails();
$filemanager->setUrl('test.zip'); // set a file path
$filemanager->decompress(true); // decompress file to current location and delete compressed file after
if($filemanager->fails()) {
//file decompressing failed
}
setUrl method. However, this must be used to set source path when
trying to rename folders or paths.
$filemanager->source($path, $extensions);
where:
$path: full path of files to be renamed
$extensions: the extension of files allowed to be renamed
$fileManager->source(dirname(__FILE__)."/main/images","jpg");
setUrl() method can also be used to achieve a similar function by setting its
second argument as true just as shown below:
$fileManager->setUrl(dirname(__FILE__)."/main/images", true);
$fileManager->setUrl(dirname(__FILE__)."/main/images", 'jpg');
$filemanager->rename($extension);
where:
$extension: optional new file extension name.
$filemanager->source(__FILE__."/images", 'jpg'); // set source directory and only select .jpg files $filemanager->rename('png'); // rename selected files in directory to .png extension
$filemanager->prefix($path, $strict);
where:
$path: full path of files to be renamed
$strict (optional) : determines if $path must be prepended
$filemanager->source(dirname(__FILE__)."/main/images/pexels","jpg");
$filemanager->prefix("list", true); //set a prefix for file names
$filemanager->rename('png');
reNumber method directs the FileManger to allow
rename() to re-number files in a directory.
$filemanager->reNumber($bool);
where:
$bool : boolean of false prevents renaming. Default set is true
$filemanager->source(__FILE__."/images", "jpg"); $filemanager->reNumber(); //allow re-numbering of files $filemanager->rename(); // renumber all selected files serially
$filemanager->source(__FILE__."/images", "jpg");
$filemanager->prefix('list'); //add a prefix
$filemanager->reNumber(); //allow re-numbering of files
$filemanager->rename(); // renumber all selected files serially
prefix() method does not need a second argument in this case.
"list", assuming
the startFrom() method is applied,
with an integer of 5 ( i.e startFrom(5) ), by default each file is expected to be renamed
from list1.jpg - list5.jpg but the application of startFrom(5) will start
renaming from integer value 5. Hence, we will have list5.jpg - list15.jpg.
$filemanager->startFrom($counter);
where $counter: index from which renaming should be started
$filemanager->source(__FILE__."/images", "jpg");
$filemanager->prefix("list"); // add prefix to file name when renaming (prefix)
$filemanager->startFrom(5); // start counter from 5
$filemanager->rename(); // list5.jpg, list6.jpg, ...
reSpace method directs the FileManager
to replace multiple spaces in file names when renaming
$filemanager->reSpace($character);
where $character: new character that spaces should be relaced with.
$filemanager->source(__FILE__."/images", "jpg");
$filemanager->reSpace('_'); // replace whitespace with underscore (_)
$filemanager->rename(); // list1.jpg, list2.jpg, ...
smartUrl method directs the FileManager
to reduce special characters when renaming files
$filemanager->smartUrl();
$filemanager->source(__FILE__."/images", "jpg");
$filemanager->smartUrl('_'); // reduce special characters)
$filemanager->rename(); // list1.jpg, list2.jpg, ...
dirFiles method returns the files within a directory
$filemanager->dirFiles($extensions, $bool);
where:
$extensions: file extensions to be shown
$bool : boolean of true shows the full path of file when listing files
$filemanager->source(__FILE__."/images", "jpg");
$filemanager->dirFiles('jpg'); // return jpg files in a directory
view() method Resolve renaming to display mode without any active
renaming process. It prevents the rename() from actually renaming
files. This is used when previewing outputs
$filemanager->view(bool);
where:
bool : boolean of false prevents renaming. Default set is true
$filemanager->source(__FILE__."/images", "jpg"); $filemanager->view(false); //prevent re-numbering of files $filemanager->rename(); //displays expected output