The detailed annotation _php technique based on PHP file operation

Source: Internet
Author: User
Tags mkdir readfile

Copy Code code as follows:

$path 1= "E:/myphp/text.txt";
if (!file_exists ($path 1)) {
echo "file does not exist! ";
}else{
$handle 1 = fopen ($path 1, ' r+ ') or exit ("Unable to open file");
while (!feof ($handle 1)) {
Echo fgets ($handle 1). " <br> ";
// }
while (!feof ($handle 1)) {
Echo fgetc ($handle 1);
}
}

The code above illustrates a simple file read operation. Under Description:
Fopen is the open file resource.
How to use:
$file =fopen ("Welcome.txt", "R");
The first parameter is the path to the file. The following arguments are the types of files that are required to open the file:
R Read only. Start at the beginning of the file.
r+ read/write. Start at the beginning of the file.
W write only. Open and empty the contents of the file, or create a new file if the file does not exist.
w+ read/write. Open and empty the contents of the file, or create a new file if the file does not exist.
A append. Opens and writes to the end of the file file, and creates a new file if the file does not exist.
A + read/append. Maintains the contents of the file by writing to the end of the file.
X write only. Create a new file. Returns FALSE if the file already exists.
x+
Read/write. Create a new file. Returns FALSE and an error if the file already exists.

Note: Returns 0 (false) if fopen () cannot open the specified file.
More commonly used is the previous 4.
FGETC:
String Fgetc (Resource$handle)
Returns a string containing a character that is obtained from the file that the handle points to. Returns False if you encounter EOF.

Fgets
String Fgets (Int$handle [, Int$length])
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.
Returns FALSE when an error occurs.

FGETSS:
String Fgetss (Resource$handle [, Int$length [, String$allowable_tags]])
Same as fgets () except that FGETSS tries to remove any HTML and PHP tags from the read text. (As with fgets (), it's just that he filters html and PHP tags.) )
You can specify which tags are not removed with the optional third argument.
The feof () function detects whether the end of the file (EOF) has been reached.

To determine whether a file or directory exists
BOOL File_exists (string filename)
To determine whether a file or directory exists, to return true if it exists, or to return a false
Format:

Copy Code code as follows:

if (file_exists ("Hello.txt"))
{
Echo "file exists";
}
Open File

format:
fopen (Filename,mode)
Description:Opens the specified file in the specified format
FileName: the filename to open
mode:Open mode
fopen ("Hello.txt", "w");
Indicates that the Hello.txt file is opened in a write way

Write a file
Format:
Fwrite (resource,string);
Description: Adds the specified content to the open file
Resource: Open files
String: What to write
Cases:
$handle = fopen ("Hello.txt", "w")//If a, you can append data
Fwrite ($handle, "1\r\n")

Close File
Format:
Fclose ($handle)
Description: Closes the Open file
Cases:
$handle = fopen ("Hello.txt", "w");
Fclose ($handle);

Reading a row of data
Format:
fgets (int handle[,int length])
Description: Reads length-1 characters. If length is not specified, the default byte is 1KB,
If you encounter a newline, EOF, or read length-1 characters, the program terminates.
Returns False when an error occurs;
Cases:
$handle = fopen ("Hello.txt", "R");
$buffer = fgets ($handle, 1024);
Echo $handle; Output one line of information

Read the entire file
Format:
ReadFile (filename)
Description: Read the entire file and output it to the browser
Cases:
?
ReadFile ("Hello.txt");
?>

Take file size
Format:
FileSize (filename)
Description: Gets the specified file size, error returns false
Cases:
FileSize ("A.rar")

deleting files
Format:
Unlink ()
Description: Deletes a file, returns true if successful, otherwise returns false
Cases:
Unlink ("B.txt")

Create a table of contents
Format:
mkdir (dirname)
Description: Create a directory
Example: mkdir ("NewFolder"); Create a new folder under current directory

Delete Directory
Format:
RmDir (dirname)
Description: Deletes a directory
Example: RmDir ("NewFolder");

Get file name
Format:
BaseName (filepath)
Description: Returns the filename from the specified path
Cases:
BaseName ("C:\mytools\a.txt")//back to A.txt

Get File path information
PathInfo (PATH)
Note: Returns the file path information, the result is saved in the array, the array is labeled
DirName (Path), basename (filename), extension (extension)
Example: PathInfo ("C:\mytools\a.txt")

Take absolute path
Format:
Realpath (filename)
Description: Takes the absolute path of the specified file and returns False if it fails
Example: Realpath ("H.txt")//f:\apache\example\h.txt

Copying files
Format:
Copy (Source,dest)
Description: Copy the source file to the Dest place
Example: Copy ("H.txt", "Newfloder\a.txt")

Determine if it is a directory
Format:
Is_dir (filename)
Description: Determines whether a given file name is a directory. If filename exists and
is a directory, returns True, otherwise returns false.
Cases:

Copy Code code as follows:

if (Is_dir ("NewFolder"))
{
echo "is a file directory";
}

//Open Directory
Format: Opendir (PATH)
Description: Opens a specified file directory, returns a resource identifier
Example:
$hand = Opendir (".")//Open root directory

Reading directories
Format:
Readdir ($handle)
Description: Reads an open file directory stream
Readdir ($hand);

Close Directory
Format:
Closedir ($handle)
Description: Closes an Open Directory stream
Example: Closedir ($hand);

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.