File System
Table of Contents
- Introduction
- Basic Usage
- Reading a File
- Writing to a File
- Appending to a File
- Deleting a File
- Checking if Something is a File
- Checking if a File is Readable
- Checking if a File is Writable
- Copying a File
- Moving a File
- Getting a File's Directory Name
- Getting a File's Basename
- Getting a File's Name
- Getting a File's Extension
- Getting a File's Size
- Getting a File's Last Modified Time
- Getting Files in a Directory
- Getting Files with Glob
- Checking if a File or Directory Exists
- Creating a Directory
- Deleting a Directory
- Getting the List of Directories
- Copying a Directory
Introduction
Most programs interact with a computer's file system in some way. Opulence comes with the FileSystem
class to facilitate these interactions. With it, you can easily read and write files, get attributes of files, copy files and folders, and recursively delete directories, and do other common tasks.
Basic Usage
For all examples below, assume $fileSystem = new \Opulence\Files\FileSystem();
.
Reading a File
$fileSystem->read(FILE_PATH);
Writing to a File
// The third parameter is identical to PHP's file_put_contents() flags
$fileSystem->write(FILE_PATH, 'foo', \LOCK_EX);
Appending to a File
$fileSystem->append(FILE_PATH, 'foo');
Deleting a File
$fileSystem->deleteFile(FILE_PATH);
Checking if Something is a File
$fileSystem->isFile(FILE_PATH);
Checking if a File is Readable
$fileSystem->isReadable(FILE_PATH);
Checking if a File is Writable
$fileSystem->isWritable(FILE_PATH);
Copying a File
$fileSystem->copy(SOURCE_FILE, TARGET_PATH);
Moving a File
// This is analogous to "cutting" the file
$fileSystem->move(SOURCE_FILE, TARGET_PATH);
Getting a File's Directory Name
$fileSystem->getDirectoryName(FILE_PATH);
Getting a File's Basename
// This returns everything in the file name except for the path preceding it
$fileSystem->getBaseName(FILE_PATH);
Getting a File's Name
// This returns the file name without the extension
$fileSystem->getFileName(FILE_PATH);
Getting a File's Extension
$fileSystem->getExtension(FILE_PATH);
Getting a File's Size
// The size of the file in bytes
$fileSystem->getFileSize(FILE_PATH);
Getting a File's Last Modified Time
$fileSystem->getLastModified(FILE_PATH);
Getting Files in a Directory
// The second parameter determines whether or not we recurse into subdirectories
// This returns the full path of all the files found
$fileSystem->getFiles(DIRECTORY_PATH, true);
Getting Files with Glob
// See documentation for PHP's glob() function
$filesSystem->glob($pattern, $flags);
Checking if a File or Directory Exists
$fileSystem->exists(FILE_PATH);
Creating a Directory
// The second parameter is the chmod permissions
// The third parameter determines whether or not we create nested subdirectories
$fileSystem->createDirectory(DIRECTORY_PATH, 0777, true);
Deleting a Directory
// The second parameter determines whether or not we keep the directory structure
$fileSystem->deleteDirectory(DIRECTORY_PATH, false);
Getting the List of Directories
// The second parameter determines whether or not we recurse into the directories
// This returns the full path of all the directories found
$fileSystem->getDirectories(DIRECTORY_PATH, true);
Copying a Directory
$fileSystem->copyDirectory(SOURCE_DIRECTORY, TARGET_PATH);