Home  >  Article  >  Backend Development  >  PHP SPL standard library file operation (SplFileInfo and SplFileObject) examples, _PHP tutorial

PHP SPL standard library file operation (SplFileInfo and SplFileObject) examples, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:54:29905browse

Instances of file operations (SplFileInfo and SplFileObject) of the PHP SPL standard library,

PHP SPL provides two classes, SplFileInfo and SplFileObject, to handle file operations.

SplFileInfo is used to obtain file details:

Copy code The code is as follows:
$file = new SplFileInfo('foo-bar.txt');

print_r(array(
'getATime' => $file->getATime(), //Last access time
'getBasename' => $file->getBasename(), //Get basename without path
'getCTime' => $file->getCTime(), //Get the inode modification time
'getExtension' => $file->getExtension(), //File extension
'getFilename' => $file->getFilename(), //Get the file name
'getGroup' => $file->getGroup(), //Get file group
'getInode' => $file->getInode(), //Get file inode
'getLinkTarget' => $file->getLinkTarget(), //Get the file link target file
'getMTime' => $file->getMTime(), //Get the last modification time
'getOwner' => $file->getOwner(), //File owner
'getPath' => $file->getPath(), //File path without file name
'getPathInfo' => $file->getPathInfo(), //SplFileInfo object of the upper-level path
'getPathname' => $file->getPathname(), //Full path
'getPerms' => $file->getPerms(), //File permissions
'getRealPath' => $file->getRealPath(), //Absolute file path
'getSize' => $file->getSize(),//File size in bytes
'getType' => $file->getType(),//File type file dir link
'isDir' => $file->isDir(), //Is it a directory
'isFile' => $file->isFile(), //Is it a file
'isLink' => $file->isLink(), //Is it a shortcut link
'isExecutable' => $file->isExecutable(), //Whether it is executable
'isReadable' => $file->isReadable(), //Whether it is readable
'isWritable' => $file->isWritable(), //Writable
));

SplFileObject inherits SplFileInfo and implements the RecursiveIterator and SeekableIterator interfaces, which are used to traverse, search, and operate files

Traverse:
Copy code The code is as follows:
try {
foreach(new SplFileObject('foo-bar.txt') as $line) {
echo $line;
}
} catch (Exception $e) {
echo $e->getMessage();
}

Find the specified line:
Copy code The code is as follows:
try {
$file = new SplFileObject('foo-bar.txt');
$file->seek(2);
echo $file->current();
} catch (Exception $e) {
echo $e->getMessage();
}

Write to csv file:
Copy code The code is as follows:
$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 );
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/997906.htmlTechArticlePHP SPL standard library file operation (SplFileInfo and SplFileObject) examples, PHP SPL provides two SplFileInfo and SplFileObject Class to handle file operations. SplFileInfo is used to get file details...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn