我想用fopen的w+模式,但是发现不能读文件,只能写,这可把我郁闷了,r+是可以读可以写,但是每次写的时候,文件指针总是在文件结尾。x+也不行,直接报错,我用@抑制也不好使。最好能告诉我w+为啥不能读。
解决方案 »
- 求助,关于输入身份证号码获得生日和性别的PHP函数
- test
- 谁能提供一个 美国的在线统计网站 象中国的 一统 那种 ,能免费申请的
- 有谁知道ftp绝对路径如何隐藏起来吗?
- 我想用fwrite来生成静态的页面,可怎样处理分页呢??
- 奇怪?奇怪?为何用session实现的验证,只在一个浏览器中有效,在重新打开的另一个浏览器中却无效呢?请高手指教,在线等
- 自己写的Template类,有兴趣的可以交流交流.
- 如何得到mysql中要查询表中前n个记录中符合条件记录数
- php中如何取得发出请求的客户机(经过代理)的IP地址
- php如何安装
- PHP变量的非空判断
- PHP中.htaccess Rewrite的问题
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists
$flag = fgets($handle); //这里就读取不出来
if ($num = strpos($flag, ']')) { //如果是目录名称
$cname = substr($flag, 2, $num-2); //获取目录名称
$arr[$cname] = array(); //将目录名称定义为第二维数组的下标
} else {
$arr[$cname][] = $flag; //否则就将其内容放到上一个目录下
}
}
$dirPath = SVN_ROOT.ltrim(strrchr($data['pname'], '/'), '/');
if (!file_exists($dirPath)) {
mkdir($dirPath);
}
if (!file_exists($dirPath.'/authz')) {
$handle = fopen($dirPath.'/authz', 'w+b');
$write = "[/" . $data['cname'] . "]\r\n";
$write .= "@" . $data['gname'] . " = " .str_replace('n', '', $data['auth'])."\r\n";
} else {
$handle = @fopen($dirPath.'/authz', 'w+b');
$arr = array();
while (!feof($handle)) {
$flag = fgets($handle); //将authz文件里的每行记录都读取出来
if ($num = strpos($flag, ']')) { //如果是目录名称
$cname = substr($flag, 2, $num-2); //获取目录名称
$arr[$cname] = array(); //将目录名称定义为第二维数组的下标
} else {
$arr[$cname][] = $flag; //否则就将其内容放到上一个目录下
}
}
$count = count($arr[$data['cname']]);
$arr[$data['cname']][$count] = '@'.$data['gname'] . ' = ' . str_replace('n', '', $data['auth']);
$write = '';
foreach ($arr as $key => $value) {
if (!empty($key)) {
$write .= '[/' . $key ."]\r\n";
}
foreach ($value as $v) {
if ($v != "") {
$write .= $v."\r\n";
}
}
}
}
fwrite($handle, $write);
fclose($handle);
你可以用fseek 移动文件指针
function writeover($filename,$data,$method="rb+",$iflock=1,$check=1,$chmod=1){
touch($filename);
$handle = fopen($filename,$method);
$iflock && flock($handle,LOCK_EX);
fwrite($handle,$data);
$method=="rb+" && ftruncate($handle,strlen($data));
fclose($handle);
$chmod && @chmod($filename,0777);
}
function readover($filename,$method='rb',$readsize='D'){
$filedata="";
if(file_exists($filename)){
$filesize = @filesize($filename);
$readsize!='D' && $filesize = min($filesize,$readsize);
$filedata = '';
if ($handle = @fopen($filename,$method)) {
flock($handle,LOCK_SH);
$filedata = @fread($handle,$filesize);
fclose($handle);
}
}
return $filedata;
}
// read
fgets($file);
// move back to beginning of file
fseek($file,0);