PHP programming File Processing class SplFileObject and SplFileInfo usage instance analysis,

Source: Internet
Author: User
Tags flock

PHP programming File Processing class SplFileObject and SplFileInfo usage instance analysis,

This article describes the usage of SplFileObject and SplFileInfo for PHP programming file processing. We will share this with you for your reference. The details are as follows:

In addition to the following methods, php can directly call linux commands to process large files.

File Processing class:

SplFileInfo {/* Method */public _ construct (string $ file_name) public int getATime (void) // obtain the last Object Access time public string getBasename ([string $ suffix]) // obtain the basic name of the file without path information. The parameter can be a file suffix. If there is a parameter, the basic name of the file without the extension is returned. Public int getCTime (void) // returns the timestamp of the last change in the document. Public string getExtension (void) // get the file extension public SplFileInfo getFileInfo ([string $ class_name]) // return the file path and name public string getFilename (void) as an object) // get the file name, without the path public int getGroup (void) // get the group where the file is located, return group idpublic int getInode (void) // obtain the object index node public string getLinkTarget (void) // obtain the object link target public int getMTime (void) // obtain the last modification time public int getOwner (void) // obtain the file owner's public string getPath (void) // obtain the file path without the file name and the final slash public SplFileInfo getPathInfo ([string $ class_name]) // return path object public string getPathname (void) // obtain the file path public int getPerms (void) // obtain the File Permission public string getRealPath (void) // obtain the absolute path of the file, if the file does not exist, falsepublic int getSize (void) is returned. // the size of the returned file, in the unit of public string getType (void) // The type of the returned file, which may be file, link, dirpublic bool isDir (void) // check whether it is a directory. If it is set to true, false sepublic bool isExecutable (void) is returned. // check whether the file is executable and true is returned, otherwise, falsepublic bool isFile (void) is returned. // if the file exists and is a normal file (not a link), true is returned. Otherwise, falsepublic bool isLink (void) is returned) // determine whether the file is connected, not whether falsepublic bool isReadable (void) is returned. // whether the file is readable and truepublic bool isWritable (void) is returned. // whether the file can be written is determined, writable return truepublic SplFileObject openFile ([string $ open_mode = "r" [, bool $ use_include_path = false [, resource $ context = NULL]) // obtain the file object information public void setFileClass ([string $ class_name = "SplFileObject"]) public void setInfoClass ([string $ class_name = "SplFileInfo"]) public void _ toString (void) // return the file path and name in the form of a string}

Usage:

$info = new SplFileInfo($file_name);
SplFileObject {/* constant */const integer DROP_NEW_LINE = 1; const integer READ_AHEAD = 2; const integer SKIP_EMPTY = 4; const integer READ_CSV = 8; /* Method */public string | array current (void) // returns the content of the current row of the file public bool eof (void) // checks whether the file ends. If true is returned to the end, otherwise, falsepublic bool fflush (void) is returned. // The buffer content is output to the file. If the buffer content is successful, TRUE is returned, or FALSE is returned if the buffer content fails. Public string fgetc (void) // read the public array fgetcsv ([string $ delimiter = "," [, string $ enclosure = "\""[, string $ escape = "\"]) // read the csv file public string fgets (void) // read the file public string fgetss by row ([string $ allowable_tags]) // read the file by row and remove the html mark public bool flock (int $ operation [, int & $ wouldblock]) // lock or unlock the file, return true or false/* parameter: LOCK_SH shared lock (read ). LOCK_EX exclusive lock (write ). LOCK_UN releases the lock (shared or exclusive ). LOC K_NB (additional lock) If you do not want the flock () to be blocked during the lock, you should add a modified lock after the lock (not supported on windows) flock (LOCK_EX + LOCK_NB ); // exclusive lock plus additional lock flock (LOCK_UN + LOCK_NB); */public int fpassthru (void) // all data and characters after the output file pointer public int fputcsv (array $ fields) // use a one-dimensional array as one line of input csv file, returns the Written string length or falsepublic string fread (int $ length) // reads the specified number of bytes from the file, and returns the read string or falsepublic mixed fscanf (string $ format [, mixed & $...]) // read a row from the file and parse it in the specified mode./* example: $ file = new SplFi LeObject ("misc.txt"); while ($ userinfo = $ file-> fscanf ("% s") {list ($ name, $ partition Sion, $ countrycode) = $ userinfo; // Do something with $ name $ declaration sion $ countrycode} */public int fseek (int $ offset [, int $ whence = SEEK_SET]) // move the file pointer position by byte,/* SEEK_SET sets the file pointer to the specified byte location (this mode is used by default ). SEEK_CUR sets the file pointer to the current position plus the specified byte location. SEEK_END sets the file pointer to the end of the file with the specified byte location (in this case, the byte is often negative ). public array fstat (void) // get the file information and return it as an array *// * Digit subscript associated key name (from PHP 4.0.6) description: 0 dev device number-device name 1 ino inode number-inode number 2 mode inode protection mode-inode protection mode 3 nlink number of links-number of connections 4 uid userid of owner-owner user id5 gid groupid of owner-owner's group id6 rdev device type, if inode device *-device type, if it is an inode device, 7 size in bytes-number of bytes of the file size 8 atime time of last access (unix timestamp) -last access time (Unix timestamp) 9 mtime time of last modificat Ion (unix timestamp)-last modification time (Unix timestamp) 10 ctime time of last change (unix timestamp)-last modification time (Unix timestamp) 11 blksize blocksize of filesystem IO *-block size of File System IO 12 blocks number of blocks allocated-number of blocks occupied * Windows is always 0. *-Valid only for systems that support st_blksize. -1 is returned for other systems (such as Windows. */Public int ftell (void) // returns the current file location, and the file pointer location is public bool ftruncate (int $ size) // truncates the file to the specified length, if the length is greater than the file length, use null filling (the file opening method affects) public int fwrite (string $ str [, int $ length]) // write the $ str string to the file, write only $ length. Put back the number of bytes written or nullpublic array getCsvControl (void) // Gets the delimiter and enclosure character used for parsing CSV fields. public int getFlags (void) // Gets the flags set for an instance of SplFileObject as an integer. public int getMaxLineLen (void) // returns the maximum number of bytes read by a row (if set). If not set, the default value is 0 public int key (void) // obtain the current row number. Public void next (void) // move to the next line public void rewind (void) // return to the first line public void seek (int $ line_pos) // locate the specified public void setCsvControl ([string $ delimiter = "," [, string $ enclosure = "\""[, string $ escape = "\"]) public void setFlags (int $ flags) public void setMaxLineLen (int $ max_len) // sets the maximum number of bytes that a file reads from a row, if the object contains 10 characters in each line, but the maximum read value is set to public bool valid (void) // check whether it reaches the bottom of the object. If it does not reach the bottom, TRUE is returned, and false is returned .}

Usage:

$ File = new SplFileObject ("misc.txt", 'r + '); while (! $ File-> eof () {echo $ file-> current (); $ file-> next ();} // close the file object $ file = null;

Common File Processing Methods:

/*** Get the data in the specified row range of the file * @ param unknown $ filename file name * @ param number $ startLine start line * @ param number $ endLine end line * @ param string $ method * @ return multitype: */function getFileLines ($ filename, $ startLine = 1, $ endLine = 20, $ method = 'rb') {$ content = array (); $ count = $ endLine-$ startLine; $ fp = new SplFileObject ($ filename, $ method); $ fp-> seek ($ startLine-1 ); // go to row N, and the seek method parameter starts counting from 0 for ($ I = 0; $ I <= $ count; ++ $ I) {$ content [] = $ fp-> current (); // current () get the content of the current row $ fp-> next (); // if ($ fp-> eof () {array_pop ($ content); break ;}} return array_filter ($ content); // array_filter: false, null, ''}/*** get the content of the last line of the article * @ param string $ res file path/name */function get_last_line ($ res) {$ fp = fopen ($ res, 'R'); if (false = $ fp) {return 'error';} fseek ($ fp,-1, SEEK_END); $ s = ''; while ($ c = fgetc ($ fp )) ! = False) {if ($ c = "\ n" & $ s) break; $ s = $ c. $ s; fseek ($ fp,-2, SEEK_CUR);} fclose ($ fp); return $ s ;}

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.