File System

Table of Contents

  1. Introduction
  2. Basic Usage
    1. Reading a File
    2. Writing to a File
    3. Appending to a File
    4. Deleting a File
    5. Checking if Something is a File
    6. Checking if a File is Readable
    7. Checking if a File is Writable
    8. Copying a File
    9. Moving a File
    10. Getting a File's Directory Name
    11. Getting a File's Basename
    12. Getting a File's Name
    13. Getting a File's Extension
    14. Getting a File's Size
    15. Getting a File's Last Modified Time
    16. Getting Files in a Directory
    17. Getting Files with Glob
    18. Checking if a File or Directory Exists
    19. Creating a Directory
    20. Deleting a Directory
    21. Getting the List of Directories
    22. 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);