PHP file operation of Read and write ____php

Source: Internet
Author: User
Tags fread readfile

Operation of files in PHP: creating, reading, uploading, editing. PHP has mentioned a number of related functions. 1, ReadFile ()

int ReadFile (String $filename [, bool $use _include_path = False [, resource $context]])

Parameters: filename: filename; use_include_path: Whether the file is also searched in include_path ; Context: An environment that prescribes a file handle. The context is a set of options that can modify the behavior of a stream.

Return value: Returns the number of bytes read from the file. If the error returns false and an error message is displayed unless invoked in the form of @readfile ().
Example:
1.php and New.txt in the same folder

1.php content:

<?php
Echo @readfile ("New.txt");
? >

New.txt content:

It ' s a new file.

Run 1.php results:

It ' s a new file.16 2, fopen ()

Resource fopen (String $filename, String $mode [, bool $use _include_path = False [, resource $context]])

Parameter: filename: If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet registered the encapsulation protocol, PHP will issue a message to help check for potential problems in the script and continue with filename as a normal filename. Mode: Specifies the type of access required to the stream; Use_include_path: Whether the file is also searched in include_path; context : The environment that specifies the file handle. The context is a set of options that can modify the behavior of a stream.

return value:
Returns a file pointer resource when successful, and returns FALSEif the open fails.
Note: file access Type table

Mode Description
"R" read-only to open, pointing the file pointer to the file header .
"R+" read-write mode opens, pointing 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 creating it.
"W+" read-write mode opens, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try creating it.
A write to open, pointing the file pointer to the end of the file . If the file does not exist, try creating it.
"A +" read-write mode opens, pointing the file pointer to the end of the file . If the file does not exist, try creating it.
"X" creates and opens in writing , pointing the file pointer to the file header . If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. If the file does not exist, try creating it. This and specifies the O_EXCL for the underlying open (2) system call
"X+" Create and read-write to open, pointing the file pointer to the file header . If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. If the file does not exist, try creating it. This and specifies the O_EXCL for the underlying open (2) system call
3, fread ()

String fread (Resource $handle, int $length)
Reads up to length bytes from a file pointer handle

Parameters: head: File system pointer, is a typical resource (resource) created by fopen (); length: reads up to length bytes.

return value:
Returns the string read or FALSE when it fails. 4, fclose ()

BOOL fclose (Resource $handle)
Closes the specified file (good habit, reduces server resource footprint). The file pointer must be valid and opened successfully via fopen () or Fsockopen (). 5, fgets ()

String fgets (Resource $handle [, int $length])
Reads a row of data from a file. Reads a row from the file pointed to by handle and returns a string with a maximum length of length-1 bytes. Stops when a line break is encountered (including in the return value), EOF, or after the length-1 byte has been read (see what happens first). If length is not specified, the default is 1 K, or 1024 bytes. 6, feof ()

BOOL feof (Resource $handle)
Test whether the file pointer is at the end of the file. 7, fgetc ()

String fgetc (Resource $handle)
Reads a single character from a file. 8, fwrite ()

int fwrite (Resource $handle, string $string [, int $length])

Parameters: Handle: File system pointers, which are typically created by fopen (), a resource (Resource) ; string: strings to be written; length: If length is specified, it is written to the Length bytes or after the string has been written, the write stops, depending on which situation you first encounter.

return value:
Fwrite () returns the number of characters written and false when an error occurs. 9, a super simple comment wall

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.