Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

max-s-lab/php-local-filesystem

Repository files navigation

PHP Local Filesystem Library

php 7.4+ Code Coverage Badge PHPStan Level 10 Software License Total Downloads

Installation

Run command in console:

php composer.phar require max-s-lab/php-local-filesystem

or add next line to the require section of your composer.json file:

PHP 7.4

"max-s-lab/php-local-filesystem": "^1.0"

PHP 8+

"max-s-lab/php-local-filesystem": "^2.0"

Usage

Initializing

Base

You just need to set the root directory and start using:

use MaxSLab\Filesystem\Local\LocalFilesystem;

$filesystem = new LocalFilesystem('/var/www/some-directory');

Advantage

You can also set default permissions for nested directories and files.

Note: permissions MUST be set in octal mode.

Example:

$filesystem = new LocalFilesystem('/var/www/some-directory', [
    'defaultPermissions' => [
        'directory' => 0755,
        'file' => 0644,
    ],
]);

Writing to a file

Writing content to a file with the creation of a file and a directory for it.

Example:

$filesystem->writeToFile('file.txt', 'Test');

This method also allows you to set permissions for directories and file:

$filesystem->writeToFile('file.txt', 'Test', [
    'directoryPermissions' => 0777,
    'filePermissions' => 0666,
]);

You can also use this method to write a stream to a file. To do this, simply replace 'Test' with a stream.

See file_put_contents for more information about flags

Deleting a file

Example:

$filesystem->deleteFile('file.txt');

Copying a file

Copying a file with creating a directory for it.

Example:

$filesystem->copyFile('file.txt', 'directory/file.txt');

This method also allows you to set permissions for directories and file:

$filesystem->copyFile('file.txt', 'directory/file.txt', [
    'directoryPermissions' => 0777,
    'filePermissions' => 0666,
]);

Moving a file

Moving a file with creating a directory for it.

Example:

$filesystem->moveFile('file.txt', 'directory/file.txt');

This method also allows you to set permissions for directories and file:

$filesystem->moveFile('file.txt', 'directory/file.txt', [
    'directoryPermissions' => 0777,
    'filePermissions' => 0666,
]);

Basic reading file

This method will return the contents of the file as a string.

Example:

$result = $filesystem->readFile('file.txt');

See file_get_contents for more information.

Streaming file reading

This method will return the contents of the file as a stream.

Example:

$result = $filesystem->readFileAsStream('file.txt');

See fopen for more information.

Getting file params

Getting the file size

Example:

$result = $filesystem->getFileSize('file.txt');

See filesize for more information.

Detect MIME Content-type for a file

Example:

$result = $filesystem->getFileMimeType('file.txt');

See mime_content_type for more information.

Getting the file modification time

Example:

$result = $filesystem->getFileLastModifiedTime('file.txt');

See filemtime for more information.

Creating a directory

This method creates a directory recursively.

Example:

$filesystem->createDirectory('directory');

It also allows you to set permissions for the created directories:

$filesystem->createDirectory('directory', 0777);

Deleting a directory

Recursively deleting a directory along with the contained files and directories.

Example:

$filesystem->deleteDirectory('directory');

Preparing full path

Preparing full path by relative path.

Example:

// File
$result = $filesystem->prepareFullPath('file.txt');

// Directory
$result = $filesystem->prepareFullPath('directory');

Pathnames listing

Example:

$result = filesystem->listPathnames('*');

See glob for more information.

Setting up permissions

Example:

// File
$filesystem->setPermissions('file.txt', 0644);

// Directory
$filesystem->setPermissions('directory', 0755);

See chmod for more information.

Get permissions

Example:

// File
$result = $filesystem->getPermissions('file.txt');

// Directory
$result = $filesystem->getPermissions('directory');

See fileperms for more information.

Checking the existence of a file

Example:

$result = $filesystem->fileExists('file.txt');

See is_file for more information.

Checking the existence of a directory

Example:

$result = $filesystem->directoryExists('directory');

See is_dir for more information.

About

Lightweight PHP library for working with the local file system.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages