php 修改文件属性命令,PHP文件操作类(含获取文件内容及属性,更改文件,文件超出一定大小时删除)...

该PHP文件类是本人所写,主要是介绍下PHP文件操作函数的用法(方便以后复习时用),写得比较简单,有遗漏或不足之处请指出,呵呵···

class fileinfo

{

public $file;

function __construct($f)

{

$this->file = $f;

}

function createfile()

{

touch($this->file) or die("Couldn't create $this->file,sorry!");

}

function modifilefile()

{

$fo = fopen($file,"w") or die("Couldn't open $this->file,sorry!");

if($fo)

{

fwrite($fo,"It's just a test by Adam Li on ".date("D d M Y H:i:s",time())) or die("$this->file isn't writable,sorry!");

fclose($fo);

}

}

function getfileinfo()

{

if(file_exists($this->file))

{

//文件大于1024字节时删除并重新创建空文件:

if(filesize($this->file)>1024)

{

echo "";

if(unlink($this->file))

{

echo "";

touch($this->file) or die("Couldn't create ".$this->file);

}

}

//获取文件里的内容,避免再写入时被覆盖:

$con = file_get_contents($this->file);

$fo = fopen($this->file,"w") or die("Couldn't open $this->file,sorry!");

if($fo)

{

//连同文件之前的内容一起写入:

fwrite($fo,$con."
It's just a test by Adam Li on ".date("D d M Y H:i:s",time())) or die("$file isn't writable,sorry!");

fclose($fo);

}

}

else//如果文件不存在,直接创建,写入内容:

{

$this->createfile();

$this->modifilefile();

}

//以下是获取文件属性:

//获取文件大小:

echo "

文件".$this->file."大小为:".filesize($this->file)." bytes;

";

//检查是否是文件:

echo "

$this->file is ".(is_file($this->file)?"":"not ")."a file

";

//检查是否是目录:

echo "

$this->file is ".(is_dir($this->file)?"":"not ")."a directory

";

//检查文件是否可读:

echo "

$this->file is ".(is_readable($this->file)?"":"not ")."readable

";

//检查文件是否可写:

echo "

$this->file is ".(is_writable($this->file)?"":"not ")."writable

";

//检查文件是否可执行:

echo "

$this->file is ".(is_executable($this->file)?"":"not ")."executable

";

//取得文件的上次访问时间

echo "

$this->file was accessed on ".date("D d M Y H:i:s",fileatime($this->file))."

";

//取得文件修改时间

echo "

$this->file was modified on ".date("D d M Y H:i:s",filemtime($this->file))."

";

//取得文件的 inode 修改时间

echo "

$this->file was changed on ".date("D d M Y H:i:s",filectime($this->file))."

";

//显示此时文件里的所有内容:

echo "

文件里的内容是:".file_get_contents($this->file)."

";

}

}

$obj = new fileinfo("adam.txt");

$obj->getfileinfo();

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值