A detailed explanation of PHP file Operation _php Tutorial

Source: Internet
Author: User
Tags readfile
Copy CodeThe code is 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). "
";
// }
while (!feof ($handle 1)) {
Echo fgetc ($handle 1);
}
}

The code above illustrates a simple file read operation. Description under:
Fopen is to open a file resource.
How to use:
$file =fopen ("Welcome.txt", "R");
The first parameter is the path to the file. The following parameters are required to open the file in the form of several types:
R is 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, creating a new file if the file does not exist.
A + read/append. Keep the contents of the file by writing to the end of the file.
X write only. Creates a new file. Returns FALSE if the file already exists.
x+
Read/write. Creates a new file. If the file already exists, it returns FALSE and an error.

Note: if fopen () cannot open the specified file, 0 (FALSE) is returned.
The 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 handle points to. Returns False if EOF is encountered.

Fgets
String Fgets (Int$handle [, Int$length])
Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). 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 attempts to remove any HTML and PHP markup from the text that is being read. (Same as Fgets (), except that he filters HTML and PHP tags. )
You can use the optional third parameter to specify which tags are not removed.
The feof () function detects if the end of file (EOF) has been reached.

Determine if a file or directory exists
BOOL File_exists (string filename)
Determines whether a file or directory exists, returns True if it exists, or returns false
Format:
Copy the 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 file name to open
Mode : open modes
fopen ("Hello.txt", "w");
means to open the Hello.txt file as written

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

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

Reading a row of data
Format:
fgets (int handle[,int length])
Description: Reads a length-1 character. If length is not specified, the default byte is 1KB.
If you encounter a newline, EOF, or have read length-1 characters, the program terminates,
Returns False when 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: Reads the entire file and outputs it to the browser
Cases:
ReadFile ("Hello.txt");
?>

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

deleting files
Format:
Unlink ()
Description: Delete a file, return true if successful, or return false
Cases:
Unlink ("B.txt")

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

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

Get file name
Format:
BaseName (filepath)
Description: Returns the file name from the specified path
Cases:
BaseName ("C:\mytools\a.txt")//Return 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 (file name), 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 Dest
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
Returns true for the directory, otherwise false is returned.
Cases:
Copy the Code code as follows:
if (Is_dir ("NewFolder"))
{
echo "is the file directory";
}

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

Read Directory
Format:
Readdir ($handle)
Description: Read an open file directory stream
Readdir ($hand);

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

http://www.bkjia.com/PHPjc/327764.html www.bkjia.com true http://www.bkjia.com/PHPjc/327764.html techarticle Copy the 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");

  • 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.