Introduction to common file operation functions in php _ PHP Tutorial

Source: Internet
Author: User
Tags rewind
Common File operation functions in php. Summary of common functions used for php file operations, including file read/write, creation, viewing file attributes, file deletion, and other file operations. Summary of common functions used for php file operations, including file read/write, creation, viewing file attributes, file deletion, and other file operations.

Before accessing a file, we usually need to determine whether the file exists to avoid errors caused by calling a non-existent file.

Php checks whether a file has a function: file_exists (). The structure is as follows:

File_exist ($ string );

The $ string parameter is a variable that points to a file or directory. if a file or directory exists, true is returned. otherwise, false is returned.

Instance:

The code is as follows:

/* Determine Whether post. php exists */
$ File = "post. php ";
If (file_exists ($ file )){
Echo "file exists
";
}
Else {
Echo "file does not exist
";
}

/* Determine whether the images directory exists */
$ Category = "images ";
If (file_exists ($ category )){
Echo "directory exists ";
}
Else {
Echo "the directory does not exist ";
}
?>

Php provides functions for accessing file attributes to obtain the file size, type, and modification time.

Get file property functions

Function name Function Parameters and return values
Filesize ($ string) Get file size The $ string parameter is a variable that points to a file or directory. The return value of the function is an integer variable. The size of the returned file is cached. If an error occurs, false is returned. The function parameter cannot be a remote file.
Filetype ($ string) Get file type The $ string parameter is a variable that points to a file or directory. The return value of the function is a variable of the optimized type, and the returned results are cached.
Filemtime ($ string) Get file modification time The $ string parameter is a variable that points to a file or directory. The return value of the function is an integer variable, and the modification time of the object is returned.
Fileatime ($ string) Get file access time The $ string parameter is a variable that points to a file or directory. The return value of the function is an integer variable, and the object access time is returned.
Fileperms ($ string) Get file permissions The $ string parameter is a variable that points to a file or directory. The return value of the function is an integer variable. the corresponding permissions of the returned file will be cached. The function parameter cannot be a remote file.

Instance:

The code is as follows:

$ Filename = "php.txt ";
Echo filesize ($ filename )."
";
Echo filetype ($ filename )."
";
Echo date ("Y, m, d", filemtime ($ filename ))."
";
Echo date ("Y, m, d", fileatime ($ filename ))."
";
Echo fileperms ($ filename )."
";
?>

Before reading a file, you must open a file. the fopen () function provided by php can open a local file or a remote file. The basic structure is as follows:

Resource fopen (string $ filename, string $ mode)
The filename parameter is the name of the file to be opened. The mode parameter is used to open a file, as shown in the following table:

Description of the mode parameter in fopen ()
Mode Description
R Open the file in read-only mode and point the file pointer to the file header.
R + Open in read/write mode and point the file pointer to the file header.
W Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
W + Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
A Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
A + Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
X Create and open the file in writing mode, and point the file pointer to the file header. If the file already existsFopen ()Call failed and returnFALSEAnd generateE_WARNINGLevel error message. If the file does not exist, try to create it. This corresponds toOpen (2)System callO_EXCL | O_CREATTags are equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
X + Create and open the file in read/write mode, and point the file pointer to the file header. If the file already existsFopen ()Call failed and returnFALSEAnd generateE_WARNINGLevel error message. If the file does not exist, try to create it. This corresponds toOpen (2)System callO_EXCL | O_CREATTags are equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

The fopen () function returns a value that contains an integer of the file handle and identifies the file to the function that executes file operations. This value is usually called a pointer, which is like a door in a room in a memory address. If php fails to open the file, the value is flase.

Instance:

The code is as follows:


Fopen ("php.txt", "a");/* open the local file in writing mode */
Fopen ("http://www.bKjia. c0m/robots.txt", "r");/* read-only to open remote server files */
?>


First, create a php.txt file with the following content:


Hello

Php
1. the fgetc () function reads a character from a file in the following structure:

String fgetc (resource $ handle)

The $ handle parameter is an opened file pointer. the function returns the characters pointed to by the current file pointer. If the object pointer points to the end of the object, false is returned.

Instance:

The code is as follows:

Using filename=fopen('php.txt ', 'r ');
$ String = fgetc ($ filename);/* read the first character starting with the file */
Echo $ string;
?>

After the file is opened, the file pointer is usually located at the beginning of the file. However, after performing certain operations on the file, it is difficult to determine the position of the php pointer at this time.

The PHP file pointer position lookup function ftell (). its structure is as follows:


Int ftell (resource $ handle)
The $ handle parameter is the file for pointer search. it can be used to determine the position of the file pointer. the function returns an integer.

Instance:

The code is as follows:

$ F = fopen ("php.txt", "r ");
Fgets ($ f, 2 );
Echo ftell ($ f );
?>

The php file writing function fwrite () can write the required content to the target file. The structure is as follows:


Int fwrite (resource $ handle, string $ string [, int $ length])

The $ handle parameter is the file to be written. The $ string parameter is optional and is the length to be written. The fwrite () function returns the number of written characters. If an error occurs, false is returned.

Instance:

The code is as follows:

$ Filename = "php.txt ";
$ Str1 = "first write
";
$ Str2 = "second write ";
If (is_writable ($ filename) {/* is_writable () function to determine whether a file can be written */
$ File = fopen ($ filename, "w");/* open the file in writing mode */
$ W1 = fwrite ($ file, $ str1);/* write content to a file */
$ W2 = fwrite ($ file, $ str2 );
$ File = fopen ($ filename, "r ");
If ($ w1) echo fgets ($ file);/* read file content */
Else echo "writing failed ";
}
Else echo "file not writable ";
?>


The php pointer function rewind () can set the file position pointer to the beginning of the file. its structure is as follows:


Bool rewind (resource $ handle );

The function returns a boolean value. if the operation succeeds, true is returned. if the operation fails, false is returned.

Instance:

The code is as follows:

$ F = fopen ("php.txt", "r ");
Echo fgets ($ f )."
";/* Output the first line */
Echo fgets ($ f )."
";/* Output the second line */
Rewind ($ f);/* pointer returns the file header */
Echo fgets ($ f);/* output the first line */
?>

....

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.