The Filesystem component is a small package that provides convenient utilities for common file system operations. It also abstracts the different behavior of operating systems for some of those file system functions.
In Symfony 3.2 we improved it adding two new functions.
Added a cross-platform readlink()
method
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 new readlink()
provided by the Filesystem component always behaves in
the same way:
1
public function readlink($path, $canonicalize = false)
When $canonicalize
is false
:
- if
$path
does not exist or is not a link, it returnsnull
. - 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 returnsnull
. - if
$path
exists, it returns its absolute fully resolved final version.
Added a hardlink()
method
Although most of the times developers use soft or symbolic links, sometimes it's convenient to create "hard links". The behavior of hard links is similar to the way object references work: a hard link points to the exact same inode of the target file and you can even rename the target file without breaking the hard link.
The new hardlink()
method of the Filesystem component lets you create single
or multiple hard links to a given file:
1 2 3 4 5 6 7 8
use Symfony\Component\Filesystem\Filesystem;
$fs = new Filesystem();
$fs->hardlink('file1.txt', 'hardlink1.lnk');
// fileinode('file1.txt') === fileinode('hardlink1.lnk')
// multiple hardlinks pointing to the same file
$fs->hardlink('file2.txt', ['hardlink2.lnk', 'hardlink3.lnk']);
Nice work! Thanks \o/