您的位置:首页 > 编程语言 > PHP开发

PHP之文件系统处理_文件操作…

2013-08-16 15:05 381 查看
原文地址:PHP之文件系统处理_文件操作相关的函数二作者:让拖鞋再飞一会儿fopen函数:

作用是打开文件或者
URL ,使用方法resource
fopen ( string
$filename , string
$mode [, bool
$use_include_path [, resource
$zcontext ]] )。

[b]fopen()mode
的可能值列表[/b]
mode说明
'r'只读方式打开,将文件指针指向文件头。
'r+'读写方式打开,将文件指针指向文件头。
'w'写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'w+'读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'a'写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'a+'读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
b 以二进制模式打开文件(图,电影)

t 以文本模式打开文件

例子:

<?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()
在文件指针中定位,说明:int fseek (
resource $handle ,
int $offset [,
int $whence ] )。在与
handle 关联的文件中设定文件指针位置。新位置从文件头开始以字节数度量,是以
whence 指定的位置加上
offsetwhence 的值定义为:

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()
读取文件,说明:string fread (
int $handle ,
int $length
),fread() 从文件指针 handle 读取最多
length 个字节。该函数在读取完最多 length
个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192
个字节时就会停止读取文件,视乎先碰到哪种情况。返回所读取的字符串,如果出错返回 FALSE

<?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()
倒回文件指针位置,说明:bool rewind (
resource $handle ),将
handle 的文件位置指针设为文件流的开头。成功时返回
TRUE,或者在失败时返回 FALSE.
文件指针必须合法,并且指向由 fopen() 成功打开的文件。

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"])){

$wcon=$_POST["username"].'<->'.$_POST["tit"].'<->'.$_POST["con"].'<->'.time().'<->';
//获取所有输入信息

write($db,$wcon); //将信息写入文件

if(file_exists($db)){
//判断是否有数据文件

$rcon=read($db);

$rcon=rtrim($rcon,"<|>");
//删除最后的“<|>”

$ncon=explode("<|>",$rcon);
//以<|>标记进行分割,$ncon成为数据数组

foreach($ncon as
$con){ //通过遍历去出每条数组即每条信息

list($username,$tit,$contents,$time)=explode("<->",$con);
//将每条信息又分割为相应的变量

echo
'<b>'.$username.'</b>于';

echo
'<font color="red">'.date("Y-m-d
H:i:s",$time).'</font>发表标题为';

echo
'<u>'.$tit.'</u>的留言:';

echo
'<i>'.$contents.'</i><br
/>';

}

}

}

function
read($fileName){
//读取函数

$file=fopen($fileName,"r");

if(flock($file,LOCK_SH)){
//锁定文件后再进行读取

$con=fread($file,filesize($fileName));
//filesize("文件名")

flock($file,LOCK_UN);

}

fclose($file);
//读取完成后释放资源

return $con;

}

function
write($fileName,$con){
//写入函数

$file=fopen($fileName,"a");

if(flock($file,LOCK_EX)){
//锁定文件后再进行写入

fwrite($file,$con);

flock($file,LOCK_UN);

}

fclose($file);

}

?>

<html>

<head>

<title>简易留言板</title>

<head>

<body>

<form action="bbs.php"
method="post"><br
/>

UserName:<input type="text"
name="username"><br
/>

Title:<input type="text"
name="tit"><br
/>

Contents:<textarea
name="con"></textarea><br
/>

<input type="submit" name="sub"
value="message">

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: