alexya-framework / filesystem
Alexya的FileSystem实用工具
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-14 19:47:33 UTC
README
Alexya的文件系统组件
内容
文件操作
使用\Alexya\FileSystem\File
类,读取/写入文件非常简单。
实例化文件对象
构造函数接受用于I/O操作的文件路径作为参数,如果文件不存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\FileDoesntExist
的异常。
您可以使用接受文件路径作为参数的\Alexya\FileSystem\File::exists
方法来检查文件是否存在,如果路径存在且是文件,则返回true
(否则返回false
)。
要创建文件,请使用接受要创建的文件路径作为参数的\Alexya\FileSystem\File::make
方法,它将返回文件对象的实例。如果文件已存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\FileAlreadyExists
的异常,但是,您可以通过第二个参数(一个整数)来更改此行为,该参数指示如果路径已存在时将执行什么操作
\Alexya\FileSystem\File::MAKE_FILE_EXISTS_THROW_EXCEPTION
:抛出异常(默认)。\Alexya\FileSystem\File::MAKE_FILE_EXISTS_OVERWRITE
:删除文件并重新创建。\Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN
:打开文件。
示例
<?php if(\Alexya\FileSystem\File::exists("/tmp/test.txt")) { $file = new \Alexya\FileSystem\File("/tmp/test.txt"); } else { $file = \Alexya\FileSystem\File::make("/tmp/test.txt"); }
或更短的方式
<?php $file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN);
一旦实例化了文件对象,您就可以检索有关文件的信息,例如权限和路径信息,并读取/写入文件。
文件权限
您可以使用以下方法检查文件权限
isReadable
:如果文件具有读取权限,则返回true
。isWritable
:如果文件具有写入权限,则返回true
。isExecutable
:如果文件具有执行权限,则返回true
。
示例
<?php /** * File permissions, the same way as executing `ls -l` */ $permissions = "-"; $file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN); if($file->isReadable()) { $permissions .= "r"; } else { $permissions .= "-"; } if($file->isWritable()) { $permissions .= "w"; } else { $permissions .= "-"; } if($file->isExecutable()) { $permissions .= "x"; } else { $permissions .= "-"; }
文件信息
您可以使用以下方法检索文件信息
getName
:返回文件名,不带扩展名。getExtension
:返回文件扩展名。getBasename
:返回文件名+扩展名。getPath
:返回文件的完整路径(位置+名称+扩展名)。getLocation
:返回包含文件的目录的路径。
这些方法可以静态访问,但您必须发送文件路径作为参数。
要更改文件信息,请使用以下方法
setName
:重命名文件。setExtension
:更改文件的扩展名。setBasename
:更改文件名和扩展名。setPath
:更改文件的完整路径。setLocation
:更改文件的位置。
示例
<?php // Static calls $name = \Alexya\FileSystem\File::name("/tmp/test.txt"); // $name = "test" $extension = \Alexya\FileSystem\File::extension("/tmp/test.txt"); // $extension = "txt" $basename = \Alexya\FileSystem\File::basename("/tmp/test.txt"); // $basename = "test.txt" $path = \Alexya\FileSystem\File::path("/tmp/test.txt"); // $path = "/tmp/test.txt" $location = \Alexya\FileSystem\File::location("/tmp/test.txt"); // $location = "/tmp" // Object calls $file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN); $name = $file->getName(); // $name = "test" $extension = $file->getExtension(); // $extension = "txt" $basename = $file->getBasename(); // $basename = "test.txt" $path = $file->getPath(); // $path = "/tmp/test.txt" $location = $file->getLocation(); // $location = "/tmp" $file->setName("foo"); // File path: /tmp/foo.txt $file->setExtension("bar"); // File path: /tmp/foo.bar $file->setBasename("bar.foo"); // File path: /tmp/bar.foo $file->setPath("/test/test.txt"); // File path: /test/test.txt $file->setLocation("/home"); // File path: /home/test.txt
读取/写入文件
写入文件与以下任何一种方法一样简单
write
:使用给定参数覆盖文件内容。append
:将给定参数追加到文件中。
\Alexya\FileSystem\File
类提供多种从文件读取的方法,您可以使用以下方法
getContent
:返回文件内容。read
:从文件读取,第一个参数是要读取的字节数,第二个参数是读取前的字节数偏移量。readBetween
:读取文件的一些行,第一个参数是起始行,第二个参数是最后一行。readLine
:读取一行,参数是要读取的行号。readLinesBetween
:与readBetween
相同,但返回一个数组
而不是字符串
。
示例
<?php $file = \Alexya\FileSystem\File::make("/tmp/test.txt", \Alexya\FileSystem\File::MAKE_FILE_EXISTS_OPEN); $file->write("Foo Bar test Test"); $file->append("Bar"); $content = $file->getContent(); /* $content = "Foo Bar test TestBar" */ $firstThreeBytes = $file->read(3, 0); /* $firstThreeBytes = "Foo"; */ $nextThreeBytes = $file->read(3, 3); /* $nextThreeBytes = " Ba" */ $between = $file->readBetween(2, 4); /* $between = "Bar test" */ $thirdLine = $file->readLine(3); /* $thirdLine = "test" */ $linesBetween = $file->readLinesBetween(2, 4); /* $linesBetween = [ "Bar", "test" ] */
目录操作
类\Alexya\FileSystem\Directory
提供了一个干净的方式来与目录交互。
实例化Directory对象
构造函数接受目录路径作为参数,如果目录不存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\DirectoryDoesntExist
的异常。
您可以使用方法\Alexya\FileSystem\Directory::exists
来检查目录是否存在,该方法接受目录路径作为参数,如果路径存在且是目录(或不存在则返回false
)。
要创建目录,请使用方法\Alexya\FileSystem\Directory::make
,它接受要创建的目录路径作为参数,并返回目录对象的实例。如果目录已存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\DirectoryAlreadyExists
的异常,但您可以通过第二个参数(一个整数)来改变这种行为,该参数告诉在路径已存在时应该做什么
\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_THROW_EXCEPTION
:抛出异常(默认)。\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OVERWRITE
:删除目录并重新创建。\Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN
:打开目录。
示例
<?php if(\Alexya\FileSystem\Directory::exists("/tmp")) { $directory = new \Alexya\FileSystem\Directory("/tmp"); } else { $directory = \Alexya\FileSystem\Directory::make("/tmp"); }
或更短的方式
<?php $directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN);
实例化目录对象后,您可以检索目录的信息,例如文件和子目录。
目录文件
您可以使用方法fileExists
来检查目录中是否存在文件,参数是文件的基名(名称+扩展名),如果文件存在则返回true
(如果不存在则返回false
)。
如果您想从目录中检索文件,请使用方法getFile
,参数是文件的基名(名称+扩展名),并返回一个表示文件的\Alexya\FileSystem\File
对象。如果文件不存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\FileDoesntExists
的异常,但您可以通过第二个参数(一个整数)来改变这种行为,该参数告诉如果文件不存在时应该做什么
\Alexya\FileSystem\Directory::GET_FILE_NOT_EXISTS_THROW_EXCEPTION
:抛出异常(默认)。\Alexya\FileSystem\Directory::GET_FILE_NOT_EXISTS_CREATE
:创建文件。
方法getFiles
返回一个包含目录中所有文件的\Alexya\FileSystem\File
对象数组。
示例
<?php /* * Directory /tmp: * test (directory) * foo (directory) * bar (file) * test.txt (file) */ $directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN); $fileExists = $directory->fileExists("file"); // $fileExists = false $bar = $directory->getFile("bar"); // $bar = new \Alexya\FileSystem\File("/tmp/bar") $files = $directory->getFiles(); /* $files = [ new \Alexya\FileSystem\File("/tmp/bar"), new \Alexya\FileSystem\File("/tmp/test.txt") ] */
目录子目录
您可以使用方法directoryExists
来检查目录中是否存在目录,参数是目录的名称,如果目录存在则返回true
(如果不存在则返回false
)。
如果您想从目录中检索目录,请使用方法getDirectory
,参数是目录的名称,并返回一个表示目录的\Alexya\FileSystem\Directory
对象。如果目录不存在,它将抛出一个类型为\Alexya\FileSystem\Exceptions\DirectoryDoesntExists
的异常,但您可以通过第二个参数(一个整数)来改变这种行为,该参数告诉如果目录不存在时应该做什么
\Alexya\FileSystem\Directory::GET_DIRECTORY_NOT_EXISTS_THROW_EXCEPTION
:抛出异常(默认)。\Alexya\FileSystem\Directory::GET_DIRECTORY_NOT_EXISTS_CREATE
:创建目录。
方法getDirectories
返回一个包含目录中所有目录的\Alexya\FileSystem\Directory
对象数组。
示例
<?php /* * Directory /tmp: * test (directory) * foo (directory) * bar (file) * test.txt (file) */ $directory = \Alexya\FileSystem\Directory::make("/tmp", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN); $directoryExists = $directory->directoryExists("dir"); // $directoryExists = false $test = $directory->getDirectory("test"); // $test = new \Alexya\FileSystem\Directory("/tmp/test") $directories = $directory->getDirectories(); /* $directories = [ new \Alexya\FileSystem\Directory("/tmp/test"), new \Alexya\FileSystem\Directory("/tmp/foo") ] */
目录信息
您可以使用以下方法检索目录信息
getName
:返回目录名称。getPath
:返回目录的完整路径(位置+名称)。getLocation
:返回包含目录的目录的路径。
这些方法可以静态访问,但您必须将目录的路径作为参数发送。
要更改目录的信息,请使用以下方法
setName
:重命名目录。setPath
:更改目录的完整路径。setLocation
:更改目录的位置。
示例
<?php // Static calls $name = \Alexya\FileSystem\Directory::name("/tmp/test"); // $name = "test" $path = \Alexya\FileSystem\Directory::path("/tmp/test"); // $path = "/tmp/test" $location = \Alexya\FileSystem\Directory::location("/tmp/test"); // $location = "/tmp" // Object calls $file = \Alexya\FileSystem\Directory::make("/tmp/test", \Alexya\FileSystem\Directory::MAKE_DIRECTORY_EXISTS_OPEN); $name = $file->getName(); // $name = "test" $path = $file->getPath(); // $path = "/tmp/test" $location = $file->getLocation(); // $location = "/tmp" $file->setName("foo"); // Directory path: /tmp/foo $file->setPath("/test/test"); // Directory path: /test/test $file->setLocation("/home"); // Directory path: /home/test