Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. The Filesystem Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Installation
  • Usage
    • mkdir
    • exists
    • copy
    • touch
    • chown
    • chgrp
    • chmod
    • remove
    • rename
    • symlink
    • readlink
    • makePathRelative
    • mirror
    • isAbsolutePath
    • dumpFile
    • appendToFile
  • Error Handling
  • Learn More

The Filesystem Component

Edit this page

Warning: You are browsing the documentation for Symfony 3.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

The Filesystem Component

The Filesystem component provides basic utilities for the filesystem.

Installation

You can install the component in 2 different ways:

  • Install it via Composer (symfony/filesystem on Packagist);
  • Use the official Git repository (https://github.com/symfony/filesystem).

Then, require the vendor/autoload.php file to enable the autoloading mechanism provided by Composer. Otherwise, your application won't be able to find the classes of this Symfony component.

Usage

The Filesystem class is the unique endpoint for filesystem operations:

1
2
3
4
5
6
7
8
9
10
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

$fs = new Filesystem();

try {
    $fs->mkdir('/tmp/random/dir/'.mt_rand());
} catch (IOExceptionInterface $e) {
    echo "An error occurred while creating your directory at ".$e->getPath();
}

Note

Methods mkdir(), exists(), touch(), remove(), chmod(), chown() and chgrp() can receive a string, an array or any object implementing Traversable as the target argument.

mkdir

mkdir() creates a directory recursively. On POSIX filesystems, directories are created with a default mode value `0777`. You can use the second argument to set your own mode:

1
$fs->mkdir('/tmp/photos', 0700);

Note

You can pass an array or any Traversable object as the first argument.

Note

This function ignores already existing directories.

Note

The directory permissions are affected by the current umask. Set the umask for your webserver, use PHP's umask function or use the chmod function after the directory has been created.

exists

exists() checks for the presence of one or more files or directories and returns false if any of them is missing:

1
2
3
4
5
// this directory exists, return true
$fs->exists('/tmp/photos');

// rabbit.jpg exists, bottle.png does not exist, return false
$fs->exists(array('rabbit.jpg', 'bottle.png'));

Note

You can pass an array or any Traversable object as the first argument.

copy

copy() makes a copy of a single file (use mirror() to copy directories). If the target already exists, the file is copied only if the source modification date is later than the target. This behavior can be overridden by the third boolean argument:

1
2
3
4
5
// works only if image-ICC has been modified after image.jpg
$fs->copy('image-ICC.jpg', 'image.jpg');

// image.jpg will be overridden
$fs->copy('image-ICC.jpg', 'image.jpg', true);

touch

touch() sets access and modification time for a file. The current time is used by default. You can set your own with the second argument. The third argument is the access time:

1
2
3
4
5
6
// set modification time to the current timestamp
$fs->touch('file.txt');
// set modification time 10 seconds in the future
$fs->touch('file.txt', time() + 10);
// set access time 10 seconds in the past
$fs->touch('file.txt', time(), time() - 10);

Note

You can pass an array or any Traversable object as the first argument.

chown

chown() changes the owner of a file. The third argument is a boolean recursive option:

1
2
3
4
// set the owner of the lolcat video to www-data
$fs->chown('lolcat.mp4', 'www-data');
// change the owner of the video directory recursively
$fs->chown('/video', 'www-data', true);

Note

You can pass an array or any Traversable object as the first argument.

chgrp

chgrp() changes the group of a file. The third argument is a boolean recursive option:

1
2
3
4
// set the group of the lolcat video to nginx
$fs->chgrp('lolcat.mp4', 'nginx');
// change the group of the video directory recursively
$fs->chgrp('/video', 'nginx', true);

Note

You can pass an array or any Traversable object as the first argument.

chmod

chmod() changes the mode or permissions of a file. The fourth argument is a boolean recursive option:

1
2
3
4
// set the mode of the video to 0600
$fs->chmod('video.ogg', 0600);
// change the mod of the src directory recursively
$fs->chmod('src', 0700, 0000, true);

Note

You can pass an array or any Traversable object as the first argument.

remove

remove() deletes files, directories and symlinks:

1
$fs->remove(array('symlink', '/path/to/directory', 'activity.log'));

Note

You can pass an array or any Traversable object as the first argument.

rename

rename() changes the name of a single file or directory:

1
2
3
4
// rename a file
$fs->rename('/tmp/processed_video.ogg', '/path/to/store/video_647.ogg');
// rename a directory
$fs->rename('/tmp/files', '/path/to/store/files');

symlink

symlink() creates a symbolic link from the target to the destination. If the filesystem does not support symbolic links, a third boolean argument is available:

1
2
3
4
5
// create a symbolic link
$fs->symlink('/path/to/source', '/path/to/destination');
// duplicate the source directory if the filesystem
// does not support symbolic links
$fs->symlink('/path/to/source', '/path/to/destination', true);

readlink

3.2

The readlink() method was introduced in Symfony 3.2.

readlink() read links targets.

PHP's readlink() function returns the target of a symbolic link. However, its behavior is completely different under Windows and Unix. On Windows systems, readlink() resolves recursively the children links of a link until a final target is found. On Unix-based systems readlink() only resolves the next link.

The readlink() method provided by the Filesystem component always behaves in the same way:

1
2
3
4
5
// returns the next direct target of the link without considering the existence of the target
$fs->readlink('/path/to/link');

// returns its absolute fully resolved final version of the target (if there are nested links, they are resolved)
$fs->readlink('/path/to/link', true);

Its behavior is the following:

1
public function readlink($path, $canonicalize = false)
  • When $canonicalize is false:
    • if $path does not exist or is not a link, it returns null.
    • if $path is a link, it returns the next direct target of the link without considering the existence of the target.
  • When $canonicalize is true:
    • if $path does not exist, it returns null.
    • if $path exists, it returns its absolute fully resolved final version.

makePathRelative

makePathRelative() takes two absolute paths and returns the relative path from the second path to the first one:

1
2
3
4
5
6
7
// returns '../'
$fs->makePathRelative(
    '/var/lib/symfony/src/Symfony/',
    '/var/lib/symfony/src/Symfony/Component'
);
// returns 'videos/'
$fs->makePathRelative('/tmp/videos', '/tmp')

mirror

mirror() copies all the contents of the source directory into the target one (use the copy() method to copy single files):

1
$fs->mirror('/path/to/source', '/path/to/target');

isAbsolutePath

isAbsolutePath() returns true if the given path is absolute, false otherwise:

1
2
3
4
5
6
7
8
// return true
$fs->isAbsolutePath('/tmp');
// return true
$fs->isAbsolutePath('c:\\Windows');
// return false
$fs->isAbsolutePath('tmp');
// return false
$fs->isAbsolutePath('../dir');

dumpFile

dumpFile() saves the given contents into a file. It does this in an atomic manner: it writes a temporary file first and then moves it to the new file location when it's finished. This means that the user will always see either the complete old file or complete new file (but never a partially-written file):

1
$fs->dumpFile('file.txt', 'Hello World');

The file.txt file contains Hello World now.

appendToFile

3.3

The appendToFile() method was introduced in Symfony 3.3.

appendToFile() adds new contents at the end of some file:

1
$fs->appendToFile('logs.txt', 'Email sent to user@example.com');

If either the file or its containing directory doesn't exist, this method creates them before appending the contents.

Error Handling

Whenever something wrong happens, an exception implementing ExceptionInterface or IOExceptionInterface is thrown.

Note

An IOException is thrown if directory creation fails.

Learn More

  • LockHandler
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Get your Symfony expertise recognized

    Get your Symfony expertise recognized

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Valentin, a Symfony contributor

    Thanks Valentin for being a Symfony contributor

    1 commit • 77 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch