php study notes--advanced tutorial--reading files, creating files, writing files
Open the file: fopen: fopen(filename,mode);//fopen("test.txt","r");
Open mode: r Open in read-only mode, point the file pointer to the file header
r+ opens in reading and writing mode, pointing the file pointer to the file header
w writing mode, points to the file header, if it does not exist, try to create it
w+ reading and writing mode, points to the file header, if it does not exist, try to create it
a opens in writing mode and points to the end of the file. If it does not exist, try to create it.
a+ opens in read-write mode, points to the end of the file, if it does not exist, try to create it
Read the file: fread:fread();
readfile(filename): Read the file content and write it to the output buffer
<?php
echo readfile("webdictionary.txt");
?>
Copy after login
fopen(filename, mode): open file, create file
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Copy after login
fopen也用于创建文件,如果打开的不存在的文件,此函数会创建文件,嘉定文件被打开为写入(w)或者增加(a)。
Copy after login
模式 |
描述 |
r |
打开文件为只读。文件指针在文件的开头开始。 |
w |
打开文件为只写。删除文件的内容或创建一个新的文件,如果它不存在。文件指针在文件的开头开始。 |
a |
打开文件为只写。文件中的现有数据会被保留。文件指针在文件结尾开始。创建新的文件,如果文件不存在。 |
x |
创建新文件为只写。返回 FALSE 和错误,如果文件已存在。 |
r+ |
打开文件为读/写、文件指针在文件开头开始。 |
w+ |
打开文件为读/写。删除文件内容或创建新文件,如果它不存在。文件指针在文件开头开始。 |
a+ |
打开文件为读/写。文件中已有的数据会被保留。文件指针在文件结尾开始。创建新文件,如果它不存在。 |
x+ |
创建新文件为读/写。返回 FALSE 和错误,如果文件已存在。 |
fwrite(resource, content) //Write file content
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Copy after login
fread(): Function reads an open file.
fread($myfile,filesize("webdictionary.txt"));
Copy after login
fget(resource, length) //Get the file content, if the length is 10, you can get 9 bits
fgets(): used to read a single line, fgets(file, lenght), lenght is optional and specifies the number of bytes to be read. The default is 1024 bytes.
Reads a line from the file pointed to by file and returns a string of length at most length - 1 byte. Stops after encountering a newline character (included in the return value), EOF, or having read length - 1 bytes (it depends on which case is encountered first). If length is not specified, it defaults to
1K, or 1024 bytes.
On failure, returns false.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Copy after login
fgetc(): Read a single character
fclose(resource) //Close an open file resource
unlink(file) //Delete a file
feof(): Checks whether the end has been reached. Useful for looping over data of unknown length.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// 输出单行直到 end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
Copy after login
filesize(filename): filename is the file name, which is a string type. The result of this function will be cached. Use clearstatcache() to clear the cache.
filetype();
http://www.bkjia.com/PHPjc/851377.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851377.htmlTechArticlephp study notes - advanced tutorial - reading files, creating files, writing files Open files: fopen: fopen(filename,mode);//fopen("test.txt","r"); Open mode: r Read-only mode...