w+可以读啊r Read only. Starts at the beginning of the file 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
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist注意,w+是会清空文件内容的
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);