fopen函数:
mode | 说明 |
---|---|
'r' | 只读方式打开,将文件指针指向文件头。 |
'r+' | 读写方式打开,将文件指针指向文件头。 |
'w' | 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
'w+' | 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
'a' | 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
'a+' | 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
例子:
<?php
$handle = fopen("/home/rasmus/file.txt", "r"); //以只读模式打开
$handle = fopen("/home/rasmus/file.gif", "wb"); //以写模式打开
$handle = fopen("http://www.example.com/", "r"); //以读模式打开远程资源
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w"); //以写模式打开远程资源
?>
文件的读取函数
fread() 读取文件的指定长度字符
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
?>
fgets() 读取文件一行长度字符
<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
?>
fgetc() 丛文件指针中读取一个字符
<?php
$fp = fopen('somefile.txt', 'r');
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
echo "$charn";
}
?>
feof() 测试文件指针是否到了文件的结束位置
fclose() 关闭文件资源
文件的写入函数
fwrite() ,用法int fwrite ( resource $handle , string $string [, int $length ] ),作用 把 string 的内容写入 文件指针 handle 处。
<?php
$filename = 'test.txt';
$somecontent = "添加这些文字到文件n";
// 首先我们要确定文件存在并且可写。
if (is_writable($filename)) {
// 在这个例子里,我们将使用添加模式打开$filename,
// 因此,文件指针将会在文件的末尾,
// 那就是当我们使用fwrite()的时候,$somecontent将要写入的地方。
if (!$handle = fopen($filename, 'a')) {
echo "不能打开文件 $filename";
exit;
}
// 将$somecontent写入到我们打开的文件中。
if (fwrite($handle, $somecontent) === FALSE) {
echo "不能写入到文件 $filename";
exit;
}
echo "成功地将 $somecontent 写入到文件$filename";
fclose($handle);
} else {
echo "文件 $filename 不可写";
}
?>
文件内部指针
ftell()
<?php
// 打开文件并读些数据
$fp = fopen("/etc/passwd", "r");
$data = fgets($fp, 12);
// 现在指针到哪里了?
echo ftell($fp); // 11
fclose($fp);
?>
fseek()
- SEEK_SET - 设定位置等于 offset 字节。
- SEEK_CUR - 设定位置为当前位置加上 offset。
- SEEK_END - 设定位置为文件尾加上 offset。(要移动到文件尾之前的位置,需要给 offset 传递一个负值。)
如果没有指定 whence,默认为 SEEK_SET。
成功则返回 0;否则返回 -1。注意移动到 EOF 之后的位置不算错误。
<?php
$fp = fopen('somefile.txt', 'r');
// read some data
$data = fgets($fp, 4096);
// move back to the begining of the file
// same as rewind($fp);
fseek($fp, 0);
?>
fread()
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
?>
rewind()
list() — 把数组中的值赋给一些变量 ,说明:void list (mixed $varname , mixed $... ),像array() 一样,这不是真正的函数,而是语言结构。list() 用一步操作给一组变量进行赋值。 list() 仅能用于数字索引的数组并假定数字索引从 0 开始。
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!n";
?>
简易留言板实例:
<?php
$db="data.txt";
//判断是否有内容提交
if(isset($_POST["sub"])){
}
function read($fileName){
}
function write($fileName,$con){
}
?>
<html>
<head>
<head>
<body>
</body>
</html>