File operations in the PHPSPL standard library-PHP Tutorial

Source: Internet
Author: User
Tags spl
File operations in the PHPSPL standard library. PHPSPL standard library file operations this article mainly introduces PHPSPL standard library file operations (SplFileInfo and SplFileObject) instances, this article explains SplFileInfo for getting file details, php spl standard library file operations

This article mainly introduces the file operations (SplFileInfo and SplFileObject) instance of the php spl standard library, this article describes how SplFileInfo is used to obtain file details, traverse SplFileObject, search for specified rows, and write csv files. For more information, see

Php spl provides SplFileInfo and SplFileObject classes to process 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 (), // 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:

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 row:

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 a csv file:

The 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 );

}

Http://www.bkjia.com/PHPjc/998358.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/998358.htmlTechArticlePHP SPL standard library file operations this article mainly introduces the php spl standard library file operations (SplFileInfo and SplFileObject) instance, this article explains SplFileInfo used to obtain file details ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.