File Manipulation in PHP

Source: Internet
Author: User
Tags fread lstat php error

Accounting for 134 (experimental Class) Janga

fopen open file or URL

Resource fopen (String $filename, String $mode) open failure returns false

The mode parameter specifies the list of access types required to the stream:

Mode

Description

' R '

Read-only open, pointing the file pointer to the file header

' R+ '

Read and write mode open, point the file pointer to the file header

' W '

Write to open, point the file pointer to the file header and truncate the file size to zero if the file does not exist try to create the file

' w+ '

Read-write mode opens, points the file pointer to the file header and truncates the file size to zero, and attempts to create a file if the file does not exist

A

Write to open, point the file pointer to the end of the file, and attempt to create the file if it does not exist

' A + '

Read-write mode open, point the file pointer to the end of the file, and attempt to create the file if it does not exist

' X '

Creates and writes the file pointer to the file header, and if the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message if the file does not exist in the attempt to create the file, which and the underlying open (2) system call O_EXCL | C_creat tags are equivalent, note: Can only be used on local files

' x+ '

Creates and opens read-write, points the file pointer to the file header, and if the file already exists, the fopen () call fails and returns FALSE, generating an e_warning level error message. If the file does not exist, try to create the file, note: Can only be used for local files

fread () read files (can be used safely in binary files)

string fread (int $handle, int $lenght) reads up to length bytes from a file pointer handle. The function stops reading a file when it has read up to a maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or (after opening the user space stream) 8,192 bytes have been read. Returns the read string if an error returns false

File_get_contents to read the entire file into a string

String file_get_contents (String $filenamg) and file (), except for the file_get_contents (), reads the files into a string, The position specified by the parameter offset starts reading the length of the maxlen content, and if it fails, returns FALSE,

The file_get_contents () function is the preferred method for reading the contents of a file into a string, and if operating system support also uses memory-mapping techniques to enhance performance

File () reads the entire document into an array

The array file (Sting $filename), like file_get_contents, returns only files () as an array. Each cell in the array is the corresponding line in the file, including the newline character, if failure Flie () returns Falsel

If you want to search for files in Inclde_path (), you can use_include_path the optional parameters to 1.

fgets () reads a row from the file pointer

String fgets (int $handle) reads a row from a file pointed to by handle and returns a string that is up to length-1 bytes in length, encounters a newline character (included in the return value), EOF, or has read the length-1 byte and stops ( See the first case), if no length is specified, the default is 1k, error returns false

The file pointer must be valid and must be pointed to by fopen () or Fsockopen () to successfully open the file (and not yet closed)

fclose () Close an open file pointer

BOOL Fclose (Resource) turns the handle point to a file that is successfully returned true or false on failure

fwrite () writing files (which are safe for binary files) is equivalent to fputs ()

int fwrite (Resource $handle, string $string) fwrite () writes the contents of a string to the file pointer handle, if length is specified, when the length byte is written, or after a string is written, Writes are stopped, which is considered to be the first case

Fwrite () returns the number of characters written, or False if an error occurs.

File directory

file_exists check whether a file or directory exists

BOOL File_exists (String $filename) returns True if the file or directory specified by filename is present, otherwise false

Is_dir determines whether a given file is a directory

bool Is_dir (string $filename) If the file name exists and returns true for the directory, if Filenam is relative, the relative path is checked according to the current directory

is_readable to determine whether a given file name is readable

BOOL Is_readable (string $filename); If the file or directory specified by filename exists and is readable, returns true, remembering that PHP may only be able to access the file by running the Webseverver user name

is_writable () Determines whether a given file name can be written

BOOL Is_writable (string $filename) if the file exists and is writable, the return true,filename parameter can be a directory name that allows for a writable check

mkdir New Catalog

bool MkDir (string $pathname) attempts to create a directory specified by pathname, the default mode is 0777, which means the maximum possible access, which returns true successfully, otherwise returns false

fliesize Get File Size

int FileSize (string $filename) returns the number of bytes in the file size if an error returns false and generates a e_warning error

Opendir () Open Directory handle

Resource Opendir (Sting $filename) Open the directory handle if successful returns the resource of the directory handle, the failure returns false if path is not a valid directory or the directory cannot be opened because the permission restricts file system errors. Opendir () returns a false and generates a e_warning-level PHP error message

Rename renaming a file or directory

BOOL Rename (string $oldname, String $newname) attempts to rename Oldname to NewName success returns true Failure returns false

Readdir () reads entries from the directory handle

String Readdir (Resource $dir _handle) returns the file name of the next file in the directory, the file name is returned as a sort in the filesystem, the file name is returned successfully, and false on failure

rmdir Delete directory

bool RmDir(string $dirname) attempts to delete the directory specified by DirName, which must be empty, and has the appropriate permissions to return TRUE if successful, or false on failure

Unlink Deleting files

BOOL unlink (string $filename) deletes the filename. Similar to the unlink () function of Unix C. Returns TRUEon success, or FALSE on failure.

FEFO Test If the file pointer is at the end of the file

BOOL Feof (Resource $handle) returns an error (including the socket timeout) if the pointer is to EOF or if an error occurs, otherwise returns false.

Note: The file pointer must be valid and must point to a file that was successfully opened by fopen () or Fsockopne () (and not yet closed)

File permissions

chmod changing file permissions

BOOL Chmod (string $filename, int $mode)

Attempts to change the schema of the file specified by the filename to the specified mode

Note that mode is not automatically treated as an octal value, and it cannot be used as a string, and you need to add 0 to the mode before you can ensure proper operation.

Mode parameter contains three octal numbers specify the owner, the group in which the owner resides, and the access restrictions for everyone in order

chgrp Changing the group to which the file belongs

BOOL chgrp (string $filename, mixed $group)

Attempt to Change the group to which the file filename belongs (specified by group name or group ID). Only Superuser can modify the file's group arbitrarily, and other users may only change the group of files to the group that the user is in. Returns TRUEon success, or FALSE on Failure

Chown changing the owner of a file

BOOL chown (string $filename, mixed $user)

An attempt was made to Change the owner of the file filename to the user (specified by user name or user ID). Only super users can change the owner of the file. Returns TRUEon success, or FALSE on failure.

fileperms permission to obtain a file

int fileperms (string $filename) returns access to the file and returns FALSE if an error occurs .

Information about the file

Stat gives information about the file

Array stat (string $filename)

Gets the statistics for the file specified by the filename, if filename is a symbolic connection, the statistics are about the connected file itself, not the symbolic connection. If an error occurs, stat () returns false and a warning is issued

Returns an array containing the statistics for the file that has the cells listed below, with the array subscript starting from zero

stat () and Fstat () return format

Digital subscript

Association key name (from PHP 4.0.6)

Description

0

Dev

Device number-unit name

1

Ino

Inode Number-inode Number

2

Mode

Inode Protection Mode-inode Protection mode

3

Nlink

Number of links-connected

4

Uid

UserID of owner-owner's user ID

5

Gid

GroupID of owner-owner's group ID

6

Rdev

Devices type, if inode device *-device type, if it is an inode device

7

Size

Size in bytes-number of bytes in file size

8

Atime

Time of last Access (Unix timestamp)-Previous accessed (Unix timestamp)

9

Mtime

Time Modification (Unix timestamp)-Last modified (Unix timestamp)

10

CTime

Time of last Change (Unix timestamp)-Times changed (Unix timestamp)

11

Blksize

BlockSize of FileSystem IO *-Block size of file system IO

12

Blocks

Number of blocks allocated-numbers of occupied blocks

Lstat gives information about a file or symbolic connection

Array lstat (string $filename)

Gets the statistics for the file or symbol connection specified by filename. This function is the same as the stat () function except that if the filename argument is a symbolic connection, the state of the symbolic connection is returned, not the state of the file to which the symbolic connection points.

Fstat obtaining file information from an open file pointer

Array fstat (Resource $handle)

Gets The statistics for the file opened by the file pointer handle . This function is similar to the stat () function except that it acts on an open file pointer instead of a filename.

Touth setting file access and modification times

BOOL Touch (String $filename, [, int $time [, int $atime]])

Attempts to Set the access and modification time of the file given by filename to the given time. If no optional parameter is given , the current system time is used. If the third parameter atime is given , the access time for the given file is set to atime. Note that access time is always modified, regardless of the number of parameters, if the file does not exist, it will be created. Returns TRUEon success, or FALSE on failure.

Filectime Get the Inode modification time of the file

The int filectime (string $filenaem) returns the time that the file last Inode was modified and returns FALSE if an error occurs . Time is returned as a Unix timestamp.

Filemtime Get File modification time

int filemtime (string $filename) returns the last time the file was modified, and returns FALSEOn error. Time is returned as a Unix timestamp and can be used with date ().

File Manipulation in PHP

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.