FileSystem
Alexya's filesystem components, (*1)
Contents
, (*2)
File Operations
Reading/Writing to a file is really easy with the class \Alexya\FileSystem\File
., (*3)
, (*4)
Instancing File objects
The constructor accepts as parameter the path to the file that will be used for I/O operations, if the file doesn't
exist it will throw an exception of type \Alexya\FileSystem\Exceptions\FileDoesntExist
., (*5)
You can check if a file exists with the method \Alexya\FileSystem\File::exists
which accepts as parameter the path
to the file and returns true
if the path exists and is a file (or false
if it doesn't)., (*6)
To make a file use the method \Alexya\FileSystem\File::make
which accepts as parameter the path to the file that
will be created and returns an instance of the file object. If the file already exists it will throw an exception of
type \Alexya\FileSystem\Exceptions\FileAlreadyExists
, however, you can change this behavior with the second parameter
that is an integer and tells what to do if the path already exists:, (*7)
-
\Alexya\FileSystem\File::MAKE_FILE_EXISTS_THROW_EXCEPTION
: throws an exception (default).
-
\Alexya\FileSystem\File::MAKE_FILE_EXISTS_OVERWRITE
: deletes the file and recreates it.
-
\Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN
: opens the file.
Example:, (*8)
<?php
if(\Alexya\FileSystem\File::exists("/tmp/test.txt")) {
$file = new \Alexya\FileSystem\File("/tmp/test.txt");
} else {
$file = \Alexya\FileSystem\File::make("/tmp/test.txt");
}
Or a shorter way:, (*9)
<?php
$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);
Once you've instanced the file object you can retrieve information of the file such as permissions and path information
and read/write to the file., (*10)
, (*11)
File permissions
You can check file permissions with the following methods:, (*12)
-
isReadable
, returns true
if the file has read permissions.
-
isWritable
, returns true
if the file has write permissions.
-
isExecutable
, returns true
if the file has execution permissions.
Example:, (*13)
<?php
/**
* File permissions, the same way as executing `ls -l`
*/
$permissions = "-";
$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);
if($file->isReadable()) {
$permissions .= "r";
} else {
$permissions .= "-";
}
if($file->isWritable()) {
$permissions .= "w";
} else {
$permissions .= "-";
}
if($file->isExecutable()) {
$permissions .= "x";
} else {
$permissions .= "-";
}
, (*14)
You can use the followin methods for retrieving file information:, (*15)
-
getName
: Returns file name, without extension.
-
getExtension
: Returns file extension.
-
getBasename
: Returns file name + extension.
-
getPath
: Returns full path to the file (location + name + extension).
-
getLocation
: Returns path to the directory that contains the file.
The methods can be accessed statically, but you must send the path to the file as the parameter., (*16)
To change the information of a file use the following methods:, (*17)
-
setName
: Renames a file.
-
setExtension
: Changes file's extension.
-
setBasename
: Changes file name and extension.
-
setPath
: Changes full path to the file.
-
setLocation
: Changes the location to the file.
Example:, (*18)
<?php
// Static calls
$name = \Alexya\FileSystem\File::name("/tmp/test.txt"); // $name = "test"
$extension = \Alexya\FileSystem\File::extension("/tmp/test.txt"); // $extension = "txt"
$basename = \Alexya\FileSystem\File::basename("/tmp/test.txt"); // $basename = "test.txt"
$path = \Alexya\FileSystem\File::path("/tmp/test.txt"); // $path = "/tmp/test.txt"
$location = \Alexya\FileSystem\File::location("/tmp/test.txt"); // $location = "/tmp"
// Object calls
$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);
$name = $file->getName(); // $name = "test"
$extension = $file->getExtension(); // $extension = "txt"
$basename = $file->getBasename(); // $basename = "test.txt"
$path = $file->getPath(); // $path = "/tmp/test.txt"
$location = $file->getLocation(); // $location = "/tmp"
$file->setName("foo"); // File path: /tmp/foo.txt
$file->setExtension("bar"); // File path: /tmp/foo.bar
$file->setBasename("bar.foo"); // File path: /tmp/bar.foo
$file->setPath("/test/test.txt"); // File path: /test/test.txt
$file->setLocation("/home"); // File path: /home/test.txt
, (*19)
Reading/Writing to a file
Writing to a file is as easy as using any of the following methods:, (*20)
-
write
: Overwrites file contents with given parameter.
-
append
: Appends given parameter to the file.
The \Alexya\FileSystem\File
class offers numerous ways for reading from a file, you can use the following methods:, (*21)
-
getContent
: Returns the content of the file.
-
read
: Reads from a file, the first parameter is the amount of bytes to read, the second is the offset of bytes to wait before reading.
-
readBetween
: Reads some lines of the file, the first parameter is the starting line, the second is the last line.
-
readLine
: Reads a single line, the parameter is the line number to read.
-
readLinesBetween
: Same as readBetween
but instead of returning a string
returns an array
.
Example:, (*22)
<?php
$file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);
$file->write("Foo
Bar
test
Test");
$file->append("Bar");
$content = $file->getContent();
/*
$content = "Foo
Bar
test
TestBar"
*/
$firstThreeBytes = $file->read(3, 0);
/*
$firstThreeBytes = "Foo";
*/
$nextThreeBytes = $file->read(3, 3);
/*
$nextThreeBytes = "
Ba"
*/
$between = $file->readBetween(2, 4);
/*
$between = "Bar
test"
*/
$thirdLine = $file->readLine(3);
/*
$thirdLine = "test"
*/
$linesBetween = $file->readLinesBetween(2, 4);
/*
$linesBetween = [
"Bar",
"test"
]
*/
, (*23)
Directory Operations
The class \Alexya\FileSystem\Directory
offers a clean way to interact with directories., (*24)
, (*25)
Instancing Directory objects
The constructor accepts as parameter the path to the directory, if the directory doesn't exist it will throw an
exception of type \Alexya\FileSystem\Exceptions\DirectoryDoesntExist
., (*26)
You can check if a directory exists with the method \Alexya\FileSystem\Directory::exists
which accepts as parameter the path
to the directory and returns true
if the path exists and is a directory (or false
if it doesn't)., (*27)
To make a directory use the method \Alexya\FileSystem\Directory::make
which accepts as parameter the path to the directory that
will be created and returns an instance of the directory object. If the directory already exists it will throw an exception of
type \Alexya\FileSystem\Exceptions\DirectoryAlreadyExists
, however, you can change this behavior with the second parameter
that is an integer and tells what to do if the path already exists:, (*28)
-
\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_THROW_EXCEPTION
: throws an exception (default).
-
\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OVERWRITE
: deletes the directory and recreates it.
-
\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN
: opens the directory.
Example:, (*29)
<?php
if(\Alexya\FileSystem\Directory::exists("/tmp")) {
$directory = new \Alexya\FileSystem\Directory("/tmp");
} else {
$directory = \Alexya\FileSystem\Directory::make("/tmp");
}
Or a shorter way:, (*30)
<?php
$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);
Once you've instanced the directory object you can retrieve information of the directory such as files and subdirectories., (*31)
, (*32)
Directory files
You can check if a file exists in the directory with the method fileExists
, the parameter is the basename of
the file (name + extension) and returns true
if the file exists (or false
if not)., (*33)
If you want to retrieve a file from the directory use the method getFile
, the parameter is the basename of
the file (name + extension) and returns a \Alexya\FileSystem\File
object of the file. If the file doesn't exists
it will throw an exception of type \Alexya\FileSystem\Exceptions\FileDoesntExists
, however, you can change this
behavior with the second parameter that is an integer and tells what to do if the file doesn't exists:, (*34)
-
\Alexya\FileSystem\Directory::GET_FILE_NOT_EXISTS_THROW_EXCEPTION
, throws an exception (default).
-
\Alexya\FileSystem\Directory::GET_FILE_NOT_EXISTS_CREATE
, creates the file.
The method getFiles
returns an array of \Alexya\FileSystem\File
objects with all files in the directory., (*35)
Example:, (*36)
<?php
/*
* Directory /tmp:
* test (directory)
* foo (directory)
* bar (file)
* test.txt (file)
*/
$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);
$fileExists = $directory->fileExists("file"); // $fileExists = false
$bar = $directory->getFile("bar"); // $bar = new \Alexya\FileSystem\File("/tmp/bar")
$files = $directory->getFiles();
/*
$files = [
new \Alexya\FileSystem\File("/tmp/bar"),
new \Alexya\FileSystem\File("/tmp/test.txt")
]
*/
, (*37)
Directory subdirectories
You can check if a directory exists in the directory with the method directoryExists
, the parameter is the name of
the directory and returns true
if the directory exists (or false
if not)., (*38)
If you want to retrieve a directory from the directory use the method getDirectory
, the parameter is the name of
the directory and returns a \Alexya\FileSystem\Directory
object of the directory. If the directory doesn't exists
it will throw an exception of type \Alexya\FileSystem\Exceptions\DirectoryDoesntExists
, however, you can change this
behavior with the second parameter that is an integer and tells what to do if the directory doesn't exists:, (*39)
-
\Alexya\FileSystem\Directory::GET_DIRECTORY_NOT_EXISTS_THROW_EXCEPTION
, throws an exception (default).
-
\Alexya\FileSystem\Directory::GET_DIRECTORY_NOT_EXISTS_CREATE
, creates the directory.
The method getDirectories
returns an array of \Alexya\FileSystem\Directory
objects with all directories in the directory., (*40)
Example:, (*41)
<?php
/*
* Directory /tmp:
* test (directory)
* foo (directory)
* bar (file)
* test.txt (file)
*/
$directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);
$directoryExists = $directory->directoryExists("dir"); // $directoryExists = false
$test = $directory->getDirectory("test"); // $test = new \Alexya\FileSystem\Directory("/tmp/test")
$directories = $directory->getDirectories();
/*
$directories = [
new \Alexya\FileSystem\Directory("/tmp/test"),
new \Alexya\FileSystem\Directory("/tmp/foo")
]
*/
, (*42)
You can use the followin methods for retrieving directory information:, (*43)
-
getName
: Returns directory name.
-
getPath
: Returns full path to the directory (location + name).
-
getLocation
: Returns path to the directory that contains the directory.
The methods can be accessed statically, but you must send the path to the directory as the parameter., (*44)
To change the information of a directory use the following methods:, (*45)
-
setName
: Renames a directory.
-
setPath
: Changes full path to the directory.
-
setLocation
: Changes the location to the directory.
Example:, (*46)
<?php
// Static calls
$name = \Alexya\FileSystem\Directory::name("/tmp/test"); // $name = "test"
$path = \Alexya\FileSystem\Directory::path("/tmp/test"); // $path = "/tmp/test"
$location = \Alexya\FileSystem\Directory::location("/tmp/test"); // $location = "/tmp"
// Object calls
$file = \Alexya\FileSystem\Directory::make("/tmp/test", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);
$name = $file->getName(); // $name = "test"
$path = $file->getPath(); // $path = "/tmp/test"
$location = $file->getLocation(); // $location = "/tmp"
$file->setName("foo"); // Directory path: /tmp/foo
$file->setPath("/test/test"); // Directory path: /test/test
$file->setLocation("/home"); // Directory path: /home/test