Php spl standard library file operations (SplFileInfo and SplFileObject) instance,
Php spl provides SplFileInfo and SplFileObject classes to process file operations.
SplFileInfo is used to obtain file details:
Copy codeThe Code is as follows:
$ File = new SplFileInfo('foo-bar.txt ');
Print_r (array (
'Getatime' => $ file-> getATime (), // last access time
'Getbasename' => $ file-> getBasename (), // obtain the basename without a 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 a file group
'Getinode' => $ file-> getInode (), // get the file inode
'Getlinktarget' => $ file-> getLinkTarget (), // obtain the target file of the file Link
'Getmtime' => $ file-> getMTime (), // get the last modification time
'Getowner' => $ file-> getOwner (), // file owner
'Getpath' => $ file-> getPath (), // file path without a file name
'Getpathinfo' => $ file-> getPathInfo (), // The SplFileInfo object of the parent path
'Getpathname' => $ file-> getPathname (), // full path
'Getperms' => $ file-> getPerms (), // file Permission
'Getrealpath' => $ file-> getRealPath (), // absolute file path
'Getsize' => $ file-> getSize (), // file size, in bytes
'Gettype' => $ file-> getType (), // file dir link
'Isdir' => $ file-> isDir (), // whether it is a directory
'Isfile' => $ file-> isFile (), // whether it is a file
'Islink' => $ file-> isLink (), // whether it is a shortcut
'Isexecutable' => $ file-> isExecutable (), // whether it is executable
'Isreadable' => $ file-> isReadable (), // whether it is readable
'Iswritable' => $ file-> isWritable (), // whether to write
));
SplFileObject inherits SplFileInfo and implements RecursiveIterator and SeekableIterator interfaces for file traversal, searching, and operations.
Traversal:
Copy codeThe Code is as follows:
Try {
Foreach (new SplFileObject('foo-bar.txt ') as $ line ){
Echo $ line;
}
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
Find the specified row:
Copy codeThe Code is as follows:
Try {
$ File = new SplFileObject('foo-bar.txt ');
$ File-> seek (2 );
Echo $ file-> current ();
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
Write a csv file:
Copy codeThe Code is as follows:
$ List = array (
Array ('aaa', 'bbb ', 'ccc', 'ddddd '),
Array ('20140901', '20160901', '20160901 '),
Array ('"aaa"', '"bbb "')
);
$ File = new SplFileObject ('file.csv ', 'w ');
Foreach ($ list as $ fields ){
$ File-> fputcsv ($ fields );
}