Filesystem组件

3.4 版本
维护中的版本

Filesystem组件为文件系统提供基本功能。

安装 

你可以通过下述两种方式安装:

然后,包容vendor/autoload.php文件,以开启Composer提供的自动加载机制。否则,你的程序将无法找到这个Symfony组件的类。

用法 

Filesystem 类是文件系统操作的唯一入口点:

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();
}

mkdir(), exists(), touch(), remove(), chmod(), chown()chgrp() 方法可以接收一个字符串、数组、或任何实现了 Traversable 的对象,作为第二个参数。

mkdir 

mkdir() 创建一个目录。在POSIX文件系统上,目录默认被创建为 0777 模式。你可以使用第二个参数来设置自己的mode:

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

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

本方法忽略已经存在的目录。

exists 

exists() 检查所有的文件/目录是否存在,如果找不到就返回 false :

1
2
3
4
5
6
// this directory exists, return true / 目录存在,返回true
$fs->exists('/tmp/photos');
 
// rabbit.jpg exists, bottle.png does not exists, return false
// rabbit.jpg存在,bottle.png不存在,返回false
$fs->exists(array('rabbit.jpg', 'bottle.png'));

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

copy 

copy() 用于拷贝文件。如果目标已经存在,则文件只在源文件的修改日期晚于目标文件时才拷贝。这个行为可以被第三个布尔值参数所覆盖:

1
2
3
4
5
6
// works only if image-ICC has been modified after image.jpg
// 只在image-ICC的修改日期晚于image.jpg时才执行
$fs->copy('image-ICC.jpg', 'image.jpg');
 
// image.jpg will be overridden / image.jpg被覆盖了
$fs->copy('image-ICC.jpg', 'image.jpg', true);

touch 

touch() 对一个文件设置访问和修改时间。默认使用当前时间。你可以通过第二个参数设置成你自己的。第三个参数是访问时间:

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);

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

chown 

chown() 用于改变文件的owner(创建者)。第三个参数是一个布尔值的递归选项:

1
2
3
4
5
6
// set the owner of the lolcat video to www-data
// 设置lolcat视频文件的owner为www-data
$fs->chown('lolcat.mp4', 'www-data');
// change the owner of the video directory recursively
// 递归地改变视频目录的owner
$fs->chown('/video', 'www-data', true);

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

chgrp 

chgrp() 用于改变一个文件的群组。第三个参数是一个布尔值的递归选项:

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);

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

chmod 

chmod() 用于改变文件的mode(安全模式)。第四个参数是一个布尔值的递归选项:

1
2
3
4
5
// set the mode of the video to 0600 / 把视频设为0600模式
$fs->chmod('video.ogg', 0600);
// change the mod of the src directory recursively
// 递归地改变src目录的mod
$fs->chmod('src', 0700, 0000, true);

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

remove 

remove() 可以轻松地删除文件, symlinks, 目录等等:

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

你可以传入一个数组,或任何 Traversable 对象,来作为第一个参数。

rename 

rename() 用于对文件和目录重命名:

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() 创建了一个从“目标源(target)”到“目的地(destination)”的symbolic link。如果文件系统不支持symbolic links,有第三个布尔值参数可用:

1
2
3
4
5
6
// create a symbolic link / 创建一个symbolic link
$fs->symlink('/path/to/source', '/path/to/destination');
// duplicate the source directory if the filesystem
// does not support symbolic links
// 复制source目录,如果文件系统不支持symbolic links
$fs->symlink('/path/to/source', '/path/to/destination', true);

makePathRelative 

makePathRelative() 返回一个目录相对于“另一个给定目录”的路径:

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() 镜像了一个目录:

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

isAbsolutePath 

isAbsolutePath() 返回 true ,如果给定的是绝对路径的话;否则返回 false :

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() 允许你剥离出内容形成文件。它是以自动方式完成的:首先写入到一个临时文件,完成之后再把它移动到一个新的文件位置。这意味着用户始终“既能看到完整的旧文件,又能看到完整的新文件”(而决不会看到部分写入的文件):

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

file.txt 文件现在的内容是 Hello World

错误处理 

只要有错误发生,一个实现了 ExceptionInterfaceIOExceptionInterface 的异常就会被抛出。

一个 IOException 异常会在目录创建失败时抛出。

了解更多

本文,包括例程代码在内,采用的是 Creative Commons BY-SA 3.0 创作共用授权。

登录symfonychina 发表评论或留下问题(我们会尽量回复)