今天主要讲解的是php的文件处理,下面做的一些实例: 字串6
<?php
//第一种方法:
$fp = fopen("d:usr/webroot/phpceshi/a.txt",'rb');
while(!feof($fp)){
$aa = fgets($fp,1024);
echo $aa.'<br>';
}
echo '<br>'; 字串7
//第二种方法
readfile("d:usr/webroot/phpceshi/a.txt");
echo '<br>'; 字串8
//第三种方法
$name = "d:usr/webroot/phpceshi/a.txt";
$fp = fopen($name, 'rb');
fpassthru($fp);
echo '<br>';
字串2
//第四种方法
$filearray = file("d:usr/webroot/phpceshi/a.txt");
foreach ($filearray as $current)
echo '<br>'.$current.'<br>';
字串4
//读取一个字符 fgetc()从一个文件中一次读取一个字符
$fp3 = fopen("d:usr/webroot/phpceshi/a.txt",'r');
if (!$fp3) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp3))) {
echo $char.' ';
} 字串3
//读取任意长度:fread()
$handle = fopen ("http://www.baidu.com/", "rb");
do {
$data = fread($handle,8000);
if (strlen($data) == 0) {
break;
}
echo $data;
} while(true);
fclose ($handle); 字串1
//查看文件是否存在:file_exists()
if(file_exists("d:usr/webroot/phpceshi/a.txt"))
echo "ok";
else
echo "false";
echo "<br>";
echo filesize("d:usr/webroot/phpceshi/a.txt");
字串5
?>
字串3
对现有的文件操作: 字串3
<?php
//删除一个文件
$fp = fopen("d:usr/webroot/a.txt",'rb');
//将指针移动100个位置
fseek($fp,100);
//ftell调出指针当前的位置
echo '指针当前所在的位置:'.(ftell($fp)).'<br>';
//rewind可以将文件指针复位到文件的开始
rewind($fp);
echo '现在的位置在:'.(ftell($fp)).'<br>';
fclose($fp);
//锁定文件
$fp2 = fopen("d:usr/webroot/phpceshi/a.txt",'w+');
if (flock($fp2, LOCK_EX)) { // 进行排它型锁定
fwrite($fp2, "Write something hereaaa\n");
}
flock($fp2, LOCK_UN); // 释放锁定
} else {
echo "Couldn't lock the file !";
}
?>
0
顶一下0
踩一下