Home > Backend Development > PHP Tutorial > File operation of PHP SPL standard library_PHP tutorial

File operation of PHP SPL standard library_PHP tutorial

WBOY
Release: 2016-07-13 09:54:13
Original
1102 people have browsed it

File operation of PHP SPL standard library

This article mainly introduces the file operation (SplFileInfo and SplFileObject) examples of PHP SPL standard library. This article explains how SplFileInfo is used to obtain file details. Information, SplFileObject traversal, search for specified lines, writing to csv files, etc. Friends who need it can refer to it

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

SplFileInfo is used to obtain file details:

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 the file group

 'getInode' => $file->getInode(), //Get file inode

'getLinkTarget' => $file->getLinkTarget(), //Get the file link target file

'getMTime' => $file->getMTime(), //Get the last modified time

'getOwner' => $file->getOwner(), //File owner

 'getPath' => $file->getPath(), //File path without file name

 'getPathInfo' => $file->getPathInfo(), //SplFileInfo object of the superior 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(), //Whether it is a directory

'isFile' => $file->isFile(), //Whether it is a file

 'isLink' => $file->isLink(), //Whether it is a shortcut link

'isExecutable' => $file->isExecutable(), //Whether it is executable

'isReadable' => $file->isReadable(), //Whether it is readable

'isWritable' => $file->isWritable(), //Writable or not

 ));

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

Traversal:

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:

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:

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/998358.htmlTechArticleFile operations of PHP SPL standard library This article mainly introduces the file operations of PHP SPL standard library (SplFileInfo and SplFileObject ) example, this article explains how SplFileInfo is used to obtain file details,...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template