PHPackages                             filicious/ftp - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. filicious/ftp

ActiveLibrary[HTTP &amp; Networking](/categories/http)

filicious/ftp
=============

FTP adapter for filicious.

491[2 issues](https://github.com/filicious/ftp/issues)PHP

Since Jul 31Pushed 12y ago2 watchersCompare

[ Source](https://github.com/filicious/ftp)[ Packagist](https://packagist.org/packages/filicious/ftp)[ RSS](/packages/filicious-ftp/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Object oriented high level filesystem abstraction
=================================================

[](#object-oriented-high-level-filesystem-abstraction)

This is a high level filesystem abstraction for php, inspired by the Java filesystem API.

Why another filesystem abstraction?
===================================

[](#why-another-filesystem-abstraction)

We evaluated some *filesystem abstraction* frameworks, like [Gaufrette](https://github.com/KnpLabs/Gaufrette). But none of the frameworks we found, is a real *filesystem abstraction*. Gaufrette for example is more a `key => value` storage, that use a filesystem or online storage as source. Some essential functions, like delete directory are **not** available in Gaufrette. Copying files across filesystem adapters is also **not** possible.

The benefit of `php-filesystem` is that it is a unique layer that can be...

- used every time you work with files (also for temporary files)
- used across multiple filesystem (also move or copy files between each other)
- nearly complete replace the php file api
- do **not** hide the file structure
- provide high and low level functions to the filesystem
- works with php iterators
- provide a "merged" filesystem, that build a merged structure from several filesystems
- support streaming
- provide configurable public url generation (useful for web apps)

Filesystem api
==============

[](#filesystem-api)

`Filesystem` is the basic interface to access any filesystem. You need a `Filesystem` instance to connect to the filesystem and get files, but not to work with it.

`File` is the basic interface to access files and directories inside of a filesystem. A `File` instance represents a pathname inside of the underlaying filesystem. With a `File` you can do all what you want, create files and directories, delete files and directories, read and write files and list directory content including glob'ing it.

`FS` is an *static* object, to control and access global filesystem access. Currently `FS` only handle the system temporary filesystem.

`TemporaryFilesystem` is an extending interface of `Filesystem`. The `TemporaryFilesystem` provide a `createTempFile` and `createTempDirectory` method. All files/directories created with these methods will be deleted if the filesystem gets destroyed.

`Util` is a *static* object with some filesystem related methods.

`PublicUrlProvider` is an interface for a class that generated public urls for a file.

`AbstractFile` is a basic abstract implementation of `File`.

Work with the filesystem
========================

[](#work-with-the-filesystem)

Get the root `/` node of a filesystem
-------------------------------------

[](#get-the-root--node-of-a-filesystem)

```
/** @var Filesystem $fs */
/** @var File $root */
$root = $fs->getRoot();
```

Get a file from the filesystem
------------------------------

[](#get-a-file-from-the-filesystem)

```
/** @var Filesystem $fs */
/** @var File $file */
$file = $fs->getFile('/example.txt');
```

Test if file exists and test if it is a file, directory or link
---------------------------------------------------------------

[](#test-if-file-exists-and-test-if-it-is-a-file-directory-or-link)

```
/** @var File $file */
if ($file->exists()) {
	if ($file->isLink()) {
		// $file is a link
	}
	if ($file->isFile()) {
		// $file is a file
	}
	if ($file->isDirectory()) {
		// $file is a directory
	}
}
```

Get basic informations about a file
-----------------------------------

[](#get-basic-informations-about-a-file)

```
/** @var File $file */
// get the passname INSIDE of the filesystem (this may not be the real pathname)
$pathname = $file->getPathname();

// get the basename
$basename = $file->getBasename();

// the the extension
$extension = $file->getExtension();

// get the parent directory
/** @var File $parent */
$parent = $file->getParent();

// get last access time
$accessTime = $file->getAccessTime();

// get creation time
$creationTime = $file->getCreationTime();

// get last modified time
$lastModified = $file->getLastModified();

// get file size
$size = $file->getSize();

// get owner (may be the name or uid)
$owner = $file->getOwner();

// get group (may be the name or gid)
$group = $file->getGroup();
```

Get and test permissions
------------------------

[](#get-and-test-permissions)

```
/** @var File $file */
// get permissions
$mode = $file->getMode();

// test if file is readable
if ($file->isReadable()) {
	// do something...
}

// test if file is writeable
if ($file->isWriteable()) {
	// do something...
}

// test if file is executable
if ($file->isExecutable()) {
	// do something...
}
```

Delete files and directories
----------------------------

[](#delete-files-and-directories)

```
/** @var File $file */
if ($file->isDirectory()) {
	$file->delete(true); // recursive delete!!!
}
else {
	$file->delete();
}
```

Copy files
----------

[](#copy-files)

Keep in mind: `$source` and `$target` does not need to be files in the same filesystem!

```
/** @var File $source */
/** @var File $target */
$source->copyTo($target);
```

Rename/Move files
-----------------

[](#renamemove-files)

Keep in mind: `$source` and `$target` does not need to be files in the same filesystem!

```
/** @var File $source */
/** @var File $target */
$source->moveTo($target);
```

Create a directory
------------------

[](#create-a-directory)

```
/** @var File $file */
if (!$file->exists()) {
	$file->mkdir();
}
```

Create a directory path (including all missing parent directories)
------------------------------------------------------------------

[](#create-a-directory-path-including-all-missing-parent-directories)

```
/** @var File $file */
if (!$file->exists()) {
	$file->mkdirs();
}
```

Create a new empty file
-----------------------

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

```
/** @var File $file */
if (!$file->exists()) {
	$file->createNewFile();
}
```

Read and write files
--------------------

[](#read-and-write-files)

```
/** @var File $file */
// read the file
$content = $file->getContents();

// write to the file
$file->setContents("Hello world!\n");

// append to the file
$file->appendContents("The world is like a pizza!\n");
```

Truncate files
--------------

[](#truncate-files)

```
/** @var File $file */
$file->truncate(1024); // truncate to 1024 bytes
```

Streaming files
---------------

[](#streaming-files)

```
/** @var File $file */
// read the file
$stream = $file->openStream('rb');
$content = stream_get_contents($stream);
fclose($stream);

// write to the file
$stream = $file->openStream('wb');
fwrite($stream, "Hello world!\n");
fclose($stream);

// append to the file
$stream = $file->openStream('ab');
fwrite($stream, "The world is like a pizza!\n");
fclose($stream);
```

Calculate file hashes
---------------------

[](#calculate-file-hashes)

```
/** @var File $file */
// get md5 hash
$md5 = $file->hashMD5();

// get raw md5 hash
$md5raw = $file->hashMD5(true);

// get sha1 hash
$sha1 = $file->hashSHA1();

// get raw sha1 hash
$sha1raw = $file->hashSHA1(true);
```

List files in a directory
-------------------------

[](#list-files-in-a-directory)

```
/** @var File $file */
if ($file->isDirectory()) {
	// get files and directories
	$children = $file->listAll();

	// get files only
	$files = $file->ls();

	// get directories only
	$directories = $file->listDirectories();
}
```

Glob files in a directory
-------------------------

[](#glob-files-in-a-directory)

```
/** @var File $file */
if ($file->isDirectory()) {
	// get files and directories
	$children = $file->glob('*example*');

	// get files only
	$files = $file->globFiles('*example*');

	// get directories only
	$directories = $file->globDirectories('*example*');
}
```

Iterate directories (simple)
----------------------------

[](#iterate-directories-simple)

Keep in mind: the *magic* childrens `.` and `..` will never be visible to you!

```
/** @var File $file */
if ($file->isDirectory()) {
	/** @var File $child */
	foreach ($file as $child) {
		// do somethink with $child
	}
}
```

Iterate directories (expert)
----------------------------

[](#iterate-directories-expert)

Keep in mind: the *magic* childrens `.` and `..` will never be visible to you!

```
use Bit3\Filesystem\Iterator\FilesystemIterator;

/** @var File $file */
if ($file->isDirectory()) {
	$iterator = new FilesystemIterator($file, FilesystemIterator::CURRENT_AS_PATHNAME);

	/** @var string $child */
	foreach ($file as $child) {
		// $child will be the pathname
	}
}
```

Get real url to a file
----------------------

[](#get-real-url-to-a-file)

```
/** @var File $file */
$url = $file->getRealUrl();
// -> file:/real/path/to/file
// or
// -> ftp://username:password@host:port/path/to/file
// or
// ...
```

Get public url to a file
------------------------

[](#get-public-url-to-a-file)

```
/** @var File $file */
$url = $file->getPublicUrl();
// may return false|null if no public url is available
if ($url) {
	header('Location: ' . $url);
}
```

Supported filesystems
=====================

[](#supported-filesystems)

Local filesystem
----------------

[](#local-filesystem)

Allow access to the local filesystem.

```
use Bit3\Filesystem\Local\LocalFilesystem;
use Bit3\Filesystem\Iterator\RecursiveFilesystemIterator;
use RecursiveTreeIterator;

// access the filesystem
$fs = new LocalFilesystem('/path/to/directory');

// create a filesystem iterator
$filesystemIterator = new RecursiveFilesystemIterator($root, FilesystemIterator::CURRENT_AS_BASENAME);

// create a tree iterator
$treeIterator = new RecursiveTreeIterator($filesystemIterator);

// output the filesystem tree
foreach ($treeIterator as $path) {
	echo $path . "\n";
}
```

The `LocalFilesystem` constructor accept a *base path* to the root directory and an optional `PublicUrlProvider` as second argument. All files from the `LocalFilesystem` are relative to the *base path*, even absolute files.

Merged filesystem
-----------------

[](#merged-filesystem)

A merged filesystem is similar to the [union mount](http://en.wikipedia.org/wiki/Union_mount). With the merged filesystem several other filesystems can be *mounted* into a virtual structure.

```
use Bit3\Filesystem\Merged\MergedFilesystem;
use Bit3\Filesystem\Local\LocalFilesystem;
use Bit3\Filesystem\Iterator\RecursiveFilesystemIterator;
use RecursiveTreeIterator;

// create a merged filesystem
$fs = new MergedFilesystem();

// mount some other filesystems into the structure
$fs->mount('/home', new LocalFilesystem('/path/to/directory'));
$fs->mount('/remote/server', new LocalFilesystem('/other/path'));
$fs->mount('/tmp', new LocalTemporaryFilesystem('/tmp'));

// create a filesystem iterator
$filesystemIterator = new RecursiveFilesystemIterator($root, FilesystemIterator::CURRENT_AS_BASENAME);

// create a tree iterator
$treeIterator = new RecursiveTreeIterator($filesystemIterator);

// output the filesystem tree
foreach ($treeIterator as $path) {
	echo $path . "\n";
}
```

The `MergedFilesystem` constructor accept an optional filesystem object as root (/) filesystem.

FTP filesystem
--------------

[](#ftp-filesystem)

The `FTPFilesystem` allow access to an ftp server.

```
use Bit3\Filesystem\FTP\FTPFilesystemConfig;
use Bit3\Filesystem\FTP\FTPFilesystem;
use Bit3\Filesystem\Iterator\RecursiveFilesystemIterator;
use RecursiveTreeIterator;

// create a ftp configuration
$config = new FTPFilesystemConfig('example.com');
$config->setPassiveMode(true);
$config->setUsername('user');
$config->setPassword('password');
$config->setPath('/path/on/the/ftp');

// access the filesystem
$fs = new FTPFilesystem($config);

// create a filesystem iterator
$filesystemIterator = new RecursiveFilesystemIterator($root, FilesystemIterator::CURRENT_AS_BASENAME);

// create a tree iterator
$treeIterator = new RecursiveTreeIterator($filesystemIterator);

// output the filesystem tree
foreach ($treeIterator as $path) {
	echo $path . "\n";
}
```

The `FTPFilesystem` constructor accept an instance of `FTPFilesystemConfig` and an optional `PublicUrlProvider` as second argument. The `FTPFilesystemConfig` object is used, to setup the ftp configuration. The instance can be reused for several `FTPFilesystem` instantiations.

SSH Filesystem
--------------

[](#ssh-filesystem)

in work...

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e61f74ea186c1e79e072b2974ccdeef39414730476d5a8adac501eb9449b2a3?d=identicon)[tril](/maintainers/tril)

![](https://avatars.githubusercontent.com/u/44649522?v=4)[xtra](/maintainers/xtra)[@xtra](https://github.com/xtra)

---

Top Contributors

[![tristanlins](https://avatars.githubusercontent.com/u/343404?v=4)](https://github.com/tristanlins "tristanlins (38 commits)")[![backbone87](https://avatars.githubusercontent.com/u/1196313?v=4)](https://github.com/backbone87 "backbone87 (26 commits)")[![discordier](https://avatars.githubusercontent.com/u/940331?v=4)](https://github.com/discordier "discordier (23 commits)")

### Embed Badge

![Health badge](/badges/filicious-ftp/health.svg)

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M81](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.7k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k114](/packages/httpsoft-http-message)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.4M87](/packages/mezzio-mezzio-router)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)

PHPackages © 2026

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