文秘频道 | 图片频道 | 教程频道 | 教案频道 | 加入收藏 | 会员投稿
当前位置:教程荟萃首页 >> 编程开发 >> PHP >> php七天入门教程第四天
php七天入门教程第四天
2008-03-25 13:28:30  作者:  来源:互联网  浏览次数:1  文字大小:【】【】【

今天主要讲解的是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

踩一下
友情链接 | 诚聘英才 | 关于我们 | 版权声明 | 联系我们 | 广告服务
  •     大智学场copyright©2007 站长QQ:138290388 闽ICP备05004390号 
    声明:站内提供的所有教程文章均是由网上搜集或网友提供,若侵犯了你的版权利益,敬请来信通知我们!