PHPackages                             spoova/filemanager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. spoova/filemanager

ActiveLibrary[File &amp; Storage](/categories/file-storage)

spoova/filemanager
==================

Spoova package for managing files

v1.5.0(2y ago)113MITPHP

Since Mar 21Pushed 2y ago1 watchersCompare

[ Source](https://github.com/teymzz/Filemanager)[ Packagist](https://packagist.org/packages/spoova/filemanager)[ RSS](/packages/spoova-filemanager/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

FileManager
===========

[](#filemanager)

The Filemanager class has some useful features which allows the reading and writing into editable files, transferring of files and zipping of files or directories.

#### Initializing class

[](#initializing-class)

This class can be initialized as shown below

```
include_once "vendor/autoload.php";

use Spoova\FileManager\FileManager;

$Filemanager = new FileManager;
```

#### Create new directory

[](#create-new-directory)

A new directory can only be created by supplying the full path of the new directory with the `addDir()` method. This method does not also update the last directory defined within the class. It only adds a new directory if it does not exist and returns true if the directory has been created or it already exists.

```
$Filemanager->addDir('some/new/dir'); //full path of new directory
```

#### Setting url for activity

[](#setting-url-for-activity)

Aside from few methods like `addDir()` and `openFile()` methods which have stand-alone capabilities of overiding default urls, when using Filemanger for activities, a url or file path is expected to be defined which helps the Filemanager class to know where the operation is expected to be performed. A directory or file path can be specified with the `setUrl()` method. An example is shown below:

```
$Filemanager->setUrl(__DIR__); //example of setting a url
```

#### Create a new file

[](#create-a-new-file)

To create a new file, we can use the `openFile()` method. This method will only create a file if the file does not exist. A boolean of true will be returned if the file created is readable.

```
$Filemanager->openFile($strict, $path); //full path of new directory
```

- `$strict` a boolean value of true will allow creating a directory if the expected file destination does not already exist while false will prevent creating a new directory for a file if the directory does not already exist
- `$path` an optional stand-alone file path for file to be created if not earlier defined with `setUrl()`.

```
$Filemanager->openFile(true, 'some/path/of/file.txt');
```

> We can also specify the file path from the setUrl() method as shown below

```
$Filemanager->setUrl('some/path/of/file.txt');

$Filemanager->openFile(true);
```

#### Create multiple files

[](#create-multiple-files)

To create multiple files, we can use the `openFiles()` method. The first argument is an array list of full file paths of files expected to be created. The method will create directories for files if the directories do not exist and it only fails when an error is encountered and a file cannot be created. It returns true if all files were created successfully.

```
$Filemanager->openFiles($paths, $files);
```

- `$paths` array list of file paths to be created
- `$files` an optional variable that contains lists of successfully created file paths.

```
$Filemanager->openFiles(['file.txt', 'file2.txt'], $get_created_files);
```

#### Working with files and directories

[](#working-with-files-and-directories)

In order to work with a directory, the directory must be defined where an activity is expected to be performed using the `setUrl()` method. Once the source url is defined, we can proceed with the activity.

##### Set a url

[](#set-a-url)

```
$Filemanager->setUrl(__DIR__); //specify directory
```

##### Get folders in specified directory

[](#get-folders-in-specified-directory)

```
$folders = $Filemanager->getFolders(); //full path of folders
```

> Folder names only can be harvested without their full paths

```
$folders = $Filemanager->getFolders(false); // names of folders
```

##### Get files in specifed directory

[](#get-files-in-specifed-directory)

```
$files = $Filemanager->getFiles();
```

> File names and extension can be harvested without their full paths as shown below

```
$files = $Filemanager->getFiles(false); // names of files
```

> We can also get names of files without their extensions. However, it is important to always get extension names to know the type of file

```
$files = $Filemanager->getFiles(false, false); // names of files without extension name
```

##### Get contents of a directory

[](#get-contents-of-a-directory)

```
$contents = $Filemanager->getContents(); //returns both files and folders
```

##### Zip a directory

[](#zip-a-directory)

To zip a url, the `zipUrl()` methods can be applied with the syntax below

```
$folders = $Filemanager->zipUrl($output_name, $excludes);
```

- `$output_name` refers to the output name of the zip file. It uses default specified url if not defined.
- `$excludes` refers to array list of file name in specified directory that should not be added to zipped file.

> We can check if a file is zipped by using the `zipped()` method. However, this will not tell if the zipped file is broken.

```
$Filemanager->setUrl(__DIR__);
$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     echo "Zip file created."

} else {

     echo "Zip file not created!";

}
```

> We can get the last directory specified using the `lastDir()` method.

```
$Filemanager->setUrl(__DIR__);
$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     echo $Filemanager->lastDir(); //directory of zipped file

} else {

     echo "Zip file not created!";

}
```

##### Copy from a directory to another

[](#copy-from-a-directory-to-another)

To copy from last specified directory to another directory we can apply the `copyTo()` method. This will copy the entire folder to another specified directory

```
$Filemanager->copyTo($newdir, $newname, $strict);
```

- `$newdir` refers to the file or folder destination.
- `$newname` refers to new name of file. This is optional
- `$strict` boolean of true prevents any previous error from stopping operation.

```
$Filemanger->setUrl(__DIR__);

$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     $Filemanager->copyTo('some/new/directory', 'zip_one'); //copy zip file to new directory

}
```

##### Move last specified directory or file to another location

[](#move-last-specified-directory-or-file-to-another-location)

To move the last specified directory to another location, the `moveContentsTo()` can be applied. This is mostly useful when we want to move a zipped file to another location.

```
$Filemanager->moveContentsTo($newdir, $ignore, $moved);
```

- `$newdir` refers to the new file or folder destination.
- `$ignore` refers to file or folder names within the last specified url to be ignored from being moved.
- `$moved` this is used to store the names of moved files.

An example of the above is shown below

```
$Filemanger->setUrl(__DIR__);

$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     $Filemanager->moveContentsTo('some/new/directory');

}
```

##### Move last declared directory to another directory with a new name

[](#move-last-declared-directory-to-another-directory-with-a-new-name)

To move a last declared path to another directory while specifying a new output name, the `moveTo()` can be applied.

```
$Filemanager->moveTo($newdir, $name, $strict);
```

- `$newdir` refers to new destination directory where last detected path will be moved into.
- `$name` Optional. Refers to new output name of a file if specified. This is useful when $newdir is a file path and not directory. If not specified, the moved file will maintain the original file name.
- `$strict` boolean of true prevents any previous error from stopping operation.

An example of the above is shown below

```
$Filemanger->setUrl(__DIR__);

$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     $new_name = 'mydir.zip';

     $Filemanager->moveTo('some/new/directory', $new_name); //move zipped directory file to new path with new name.

}
```

##### Advanced moving of files and directories

[](#advanced-moving-of-files-and-directories)

A more advanced method for moving files or directories to another is the `move()` method. The basic syntax is show below:

```
$Filemanager->move($param1, $param2);
```

- `$param1` refers to new destination directory of selected file (or directory) or refers to the subpath of selected directory to be moved into destination directory $param2 if $param2 is defined.
- `$param2` refers to destination path of $param1 only if it is supplied.

The example shown below is that in which a directory or file was moved to another directory.

```
$Filemanger->setUrl(__DIR__.'/'.'folder_or_file_name'); //set a directory or file path

$Filemanger->move(__DIR__.'/destination_directory'); //move to new destination directory
```

> We can also specify that a directory within a primarily selected directory should be moved to another relative or absolute directory as shown below:

```
$Filemanger->setUrl(__DIR__); //set a base directory

$Filemanger->move('folder_or_file', __DIR__.'/new_directory'); //move to new existing destination directory
```

The `move()` method above will return true or false based on whether the file transfer was successful.

##### Deleting a file or directory

[](#deleting-a-file-or-directory)

To delete a folder or file, the `deleteFile()` method can be employed

```
$Filemanager->deleteFile($dir);
```

- `$dir` refers to the path of file or directory to be deleted. If not specified, it assumes the last specified directory.

An example of the above is shown below

```
$Filemanger->setUrl(__DIR__);

$Filemanager->deleteFile(); //delete directory
```

##### Deleting a file only

[](#deleting-a-file-only)

To delete a file only, the `removeFile()` method can be employed

```
$Filemanager->removeFile($dir, $check);
```

- `$dir` refers to the path of file or directory to be deleted.
- `$check` If set as true, will return true if specified file does not exist instead of false.

An example of the above is shown below

```
$Filemanger->setUrl(__DIR__);

$Filemanager->deleteFile(); //delete directory
```

#### Extract from a zip file

[](#extract-from-a-zip-file)

To extract a non-protected zipped file to a directory, the `decompress()` method can be applied.

```
$Filemanager->decompress($del, $strict);
```

- `$path` refers to source path if `$newPath` is specified or destination path if `$newPath` is not specified.
- `$strict` boolean of true prevents any previous error from stopping operation.

An example of the above is shown below

```
$Filemanger->setUrl(__DIR__);

$Filemanager->zipUrl();

if($Filemanager->zipped()) {

     $Filemanager->moveTo('some/new/directory'); //move zipped file to new path

}

$Filemanager->moveTo('some/old/path', 'some/new/path'); // move from some old to new path
```

#### Working with text readable files

[](#working-with-text-readable-files)

Reading from files is usually done by specifying a key and a separator character. By default, Filemanager uses the colon `:` character to differentiate between keys and values. In some cases the default character is set as `=` as in the case of `loadenv()` method. We can read from a readable file by using the `readFile()` and `readAll()` method.

##### Reading from text file

[](#reading-from-text-file)

```
$Filemanager->readFile($key, $separator);
```

- `$key` name of key
- `$separator` specified separator character.

> Assuming we have a file *user.txt* like below :

```
name:  Foo name;
class: Foo class;
```

> we can get the contents of the text file using the format below

```
$Filemanager->setUrl('user.txt');

$contents = $Filemanager->readFile(['name'], ":"); // ['name' => 'Foo name']
```

> By default, the delimiter `;` is usually removed whether specified or not. We can also read all contents with `readAll()` as shown below :

```
$Filemanager->setUrl('user.txt');

$contents = $Filemanager->readFile(['name', 'class']); // ['name' => 'Foo name', 'class'=> 'Foo class']
```

> If a key that does not exist is being searched, the returned value will be an empty string

```
$Filemanager->setUrl('user.txt');

$contents = $Filemanager->readFile(['user'], ":"); // ['user' => '']
```

##### Editing a text file

[](#editing-a-text-file)

There are five different ways of modifying text files which include the use of `textLine()`, `textWrite()`, `textUpdate()`, `textReplace()` and `textDelete()` methods.

###### textLine()

[](#textline)

This method is used to write a new line into text file with options before or after a specified line.

```
$Filemanager->textLine($number, $position);
```

- `$number` number of new lines to be added to text file.
- `$position` specifies where the lines should be added.

> Assuming we have a file *user.txt*, we can add new lines to the file using the format below

```
$Filemanager->setUrl('user.txt');

$contents = $Filemanager->textLine(2); // add two new lines
```

> If the file already contains a text, we can specify the position of line using `after` or `before` as shown below :

```
name:  Foo name;
class: Foo class;
```

```
$Filemanager->setUrl('user.txt');

$Filemanager->textLine(2, ['after' => 'name']); // add two lines after line of key 'name:'
```

###### textWrite()

[](#textwrite)

This method is used to write texts into a text file

```
$Filemanager->textLine($data, $options);
```

- `$data` array list of keys and values pairs
- `$options` used to define position and separator character

> Assuming we have a file *user.txt*, we can add new keys to the file using the format below :

```
$Filemanager->setUrl('user.txt');

$Filemanager->textWrite(['name' => 'foo']); // add two new lines
```

> We can specify separator character through supplied options with `separator` key. Also if the file already contain keys, we can specify the position of new keys with `before` or `after` options as shown below :

```
name:  Foo name;
class: Foo class;
```

```
$Filemanager->setUrl('user.txt');

$Filemanager->textWrite(['age' => 'foo'], ['after' => 'name', 'separator' => ':']); // add key after "name"
```

###### textUpdate()

[](#textupdate)

This method is used to update the values of already existing keys in a text file. If the specified keys do not exist, then, the keys along with their values will be added on new lines

```
$Filemanager->textUpdate($data, $upds, $separator);
```

- `$data` array list of keys and values pairs
- `$upds` used to store only updated values
- `$separator` used to set separator character. Default is `colon` sign.

> Assuming we have a file *user.txt*, we can add or update keys of the file using the format below :

```
name:  Foo name;
class: Foo class;
```

```
$Filemanager->setUrl('user.txt');

$Filemanager->textUpdate(['name' => 'Bar name', 'age' => '50']); //
```

> The resulting text file will look like the format below

```
name:  Bar name;
class: Foo class;
age: 50;
```

###### textReplace()

[](#textreplace)

This method is used to replace a key's value only if that key exists.

```
$Filemanager->textReplace($data, $separator);
```

- `$data` array list of keys and values pairs
- `$separator` used to set separator character. Default is `colon`

> Assuming we have a file *user.txt*, we can replace the existing keys' value :

```
name:  Foo name;
class: Foo class;
```

```
$Filemanager->setUrl('user.txt');

$Filemanager->textUpdate(['name' => 'Bar name']); //
```

> The resulting text file will look like the format below

```
name:  Bar name;
class: Foo class;
```

##### Reading a file without instantiating the class

[](#reading-a-file-without-instantiating-the-class)

We can read a readable file without instantiating the class itself by using the `load()` method. This method will read all the contents of a file which is equivalent to the `readAll()` method.

```
Filemanager::load($url, $separator);
```

- `$url` path of the file to be read
- `$separator` a key to value separator character

##### Reading a file into the global ENV environment

[](#reading-a-file-into-the-global-env-environment)

Readable file keys and value can be stored into the global `$_ENV` environment using the `loadenv()` method. By default, values are stored under the `$_ENV[':ENV']` key. This method is mainly used to read from `.env` files.

```
Filemanager::loadenv($url, $key, $separator);
```

- `$url` path of the file to be read
- `$key` default key is *':ENV'* . While boolean value of true will store data directly to `$_ENV`, false prevents overwriting predefined default $\_ENV key. If a custom string name is defined, the name will be used as replacement for the default ':ENV'.
- `$separator` a key to value separator character. Note that the default character for this method is set as equal sign rather than semicolon.

##### Detecting ENV key

[](#detecting-env-key)

The `env_key()` method is used to detect the last custom key used in `loadenv()` method. By default this returns `:ENV`.

##### Detecting ENV data

[](#detecting-env-data)

The `env_data()` method is used to detect the last data saved into the `$_ENV` global variable when `loadenv()` method was used.

#### Fetching errors

[](#fetching-errors)

The method `err()` returns the last error detected if an error occurs within the Filemanager system. However, when working with zip files, the `fails()` and `succeeds()` method can help to detect the error that occurs in the process of zipping or decompressing a file. Example of usage is shown below

```
$Filemanager->setUrl('some/dir');

$Filemanager->zipUrl();

if($Filemanager->succeeds()) {

     echo 'Zipping successful';

}elseif($Filemanager->fails()) {

     echo $Filemanager->error();

} else {

     echo 'Something is wrong!';

}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~54 days

Total

3

Last Release

1045d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/83e5087db5782cfe0290df66bd2871ab132d9ba620c1ac25b8e6036a71211302?d=identicon)[teymzz](/maintainers/teymzz)

---

Top Contributors

[![teymzz](https://avatars.githubusercontent.com/u/121361329?v=4)](https://github.com/teymzz "teymzz (6 commits)")

### Embed Badge

![Health badge](/badges/spoova-filemanager/health.svg)

```
[![Health](https://phpackages.com/badges/spoova-filemanager/health.svg)](https://phpackages.com/packages/spoova-filemanager)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
