PHP基础之目录和文件(7)

1.basename()
获取文件名称+扩展名
$path='C:\Appser\www\demo1.php';
echo basename($path);
返回: demo1.php

 

2.dirname()
获取路径的目录
$path='C:\Appser\www\demo1.php';
echo dirname($path);
返回: C:\Appser\www ;

3.pathinfo()
创建一个数组,获取目录名,基本名和扩展名
$path='C:\Appser\www\demo1.php';
$array_path=pathinfo($path);
print_r($array_path);

echo $array_path['basename'];//打印文件名
返回:
Array
(
[dirname] => C:\Appser\www
[basename] => demo1.php
[extension] => php
[filename] => demo1
)

4.realpath()
转换成绝对路径
$path='Demo1.php';
echo realpath($path);
输出的结果为:
C:\Appser\www\demo1.php

-------------------------------------------------------
二.
磁盘目录和文件计算
确定文件的大小:filesize()
$path='C:\Appser\www\demo1.php';
echo round(filesize($path)/1024,2) ;

计算磁盘的可用空间
disk_free_space()
$drive='C:';
echo round(disk_free_space($drive)/1024/1024,2).'MB';

计算磁盘的总空间
disk_total_space()
$drive='C:';
echo round(disk_total_space($drive)/1024/1024,2).'MB'

文件的最后访问时间
fileatime()

date_default_timezone_set('Asia/Shanghai');
echo date('Y-m-d H:i:s',fileatime('Demo1.php'));

文件的最后改变时间,所有者,权限的改变
filectime()

文件的最后修改时间
filemtime()

三.文件处理
资源(resource)
$fp=fopen(文件路径,文件模式,[是否在include_path中搜索一个文件],[允许文件名以协议开始])

模式:
r 只读
r+ 读写
w 只写
w+ 读写

a 只写(追加)
a+ 只写(追加到文件)
b 二进制
t 文本

 

----------------------------
关闭文件
fclose($fp);

写入文件
fwrite()函数 或 fputs()函数
fwrite($fp,$outputstring,[int length]);//第三个参数为最大字符数
//将保存在$outputstring 中的字符串写入到$fp指定的文件中

获取字符串的长度
strlen()

不需要资源句柄的写入方法:
file_put_contents()

"\r\n" //回车换行

读出文件
fgetc($fp) //读出字节
fgets($tp,3) //读出一行 返回长度最多为length-1字节的字符串
fgetss() //去点里面的html标记
fread(int handle,int length) //读取文件二进制
fpassthru() //输出指针处剩余的数据,不要echo,返回剩余的总长度
------------------------------
file() //每行分组存放在数组中
print_r(file('file.txt'));

readfile() //输出一个文件
readfile('file.txt');

file_get_contents()//整个文件读入一个字符串,自动关闭
echo file_get_contents('file.txt');
----------------------------------------------------------
$fp=fopen('file.txt','r');

echo fgetc($fp);

fclose($fp);
------------------------------------------
while (!feof($fp)){ //没有到达结尾打印
echo fgetc($fp);
}

------------------------------------------
file_exists() //检测文件或目录是否存在
if (file_exists('file.txt')){
echo "存在";
}else{
echo "不存在";
}
-----------------------------------
filesize('file.txt') //文件大小,字节

unlink('file.txt')//删除文件
--------------------------------
rewind() //到回到文件指针的位置 0
ftell() //返回文件指针的位置
fseek($fp,20) //在文件中在文件指针中定位


文件的锁定
flock($fp,LOCK_EX); //锁

flock($fp,LOCK_UN); //解锁

----------------------------------
opendir() //打开路径指定的目录流
readdir() //返回目录中的各个元素
closedir() //关闭目录流
sandir() //将目录读入数组
rmdir() //删除指定的目录
rename() //重命名文件


$dir = opendir( 'C:\AppServ\www\Basic5' );
while (!! $file = readdir( $dir )) {
echo $file . '<br />' ;
}
closedir( $dir );
scandir() :将目录读入数组。
print_r(scandir( 'C:\AppServ\www\Basic5' ));
rmdir() :删除指定的目录。
rmdir( 'C:\AppServ\www\Basic5\123' );
rename() :重命名文件。
rename( 'Demo1.php' , 'Demo01.php' );

posted @ 2010-12-13 11:17  phpnuke  阅读(355)  评论(0编辑  收藏  举报