PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例_php实例
PHP SPL中提供了SplFileInfo和SplFileObject两个类来处理文件操作。
SplFileInfo用来获取文件详细信息:
$file = new SplFileInfo('foo-bar.txt');
print_r(array(
'getATime' => $file->getATime(), //最后访问时间
'getBasename' => $file->getBasename(), //获取无路径的basename
'getCTime' => $file->getCTime(), //获取inode修改时间
'getExtension' => $file->getExtension(), //文件扩展名
'getFilename' => $file->getFilename(), //获取文件名
'getGroup' => $file->getGroup(), //获取文件组
'getInode' => $file->getInode(), //获取文件inode
'getLinkTarget' => $file->getLinkTarget(), //获取文件链接目标文件
'getMTime' => $file->getMTime(), //获取最后修改时间
'getOwner' => $file->getOwner(), //文件拥有者
'getPath' => $file->getPath(), //不带文件名的文件路径
'getPathInfo' => $file->getPathInfo(), //上级路径的SplFileInfo对象
'getPathname' => $file->getPathname(), //全路径
'getPerms' => $file->getPerms(), //文件权限
'getRealPath' => $file->getRealPath(), //文件绝对路径
'getSize' => $file->getSize(),//文件大小,单位字节
'getType' => $file->getType(),//文件类型 file dir link
'isDir' => $file->isDir(), //是否是目录
'isFile' => $file->isFile(), //是否是文件
'isLink' => $file->isLink(), //是否是快捷链接
'isExecutable' => $file->isExecutable(), //是否可执行
'isReadable' => $file->isReadable(), //是否可读
'isWritable' => $file->isWritable(), //是否可写
));
SplFileObject继承SplFileInfo并实现RecursiveIterator , SeekableIterator接口 ,用于对文件遍历、查找、操作
遍历:
try {
foreach(new SplFileObject('foo-bar.txt') as $line) {
echo $line;
}
} catch (Exception $e) {
echo $e->getMessage();
}
查找指定行:
try {
$file = new SplFileObject('foo-bar.txt');
$file->seek(2);
echo $file->current();
} catch (Exception $e) {
echo $e->getMessage();
}
写入csv文件:
$list = array (
array( 'aaa' , 'bbb' , 'ccc' , 'dddd' ),
array( '123' , '456' , '7891' ),
array( '"aaa"' , '"bbb"' )
);
$file = new SplFileObject ( 'file.csv' , 'w' );
foreach ( $list as $fields ) {
$file -> fputcsv ( $fields );
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
