All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET,2.0服务器控件开发之控件样式_[Asp.Net教程]
轻松掌握Ajax.net系列教程四:用Ajax.net实现客户端回调(Callback)_[Asp.Net教程]
符合web标准的媒体播放器代码_JavaScript技术_编程技术
PHP脚本的8个技巧(2)会话用法_[PHP教程]
入门:PHP与MYSQL的结合操作_php资料_编程技术
GridView中超链接根据字段不同指向不同网页_[Asp.Net教程]
C#网络应用编程基础练习题与答案(九)_[Asp.Net教程]
在c#中Windows窗体概述
让我们来编写一些PHP实用的脚本_php资料_编程技术
js去除空格trim_JavaScript技术_编程技术
Reading number is top 10 pictures
The world's ten biggest attractions of inventory super the moon
分手的感悟
迷人的靓女
移民小国也实惠2
含苞欲放的素颜美少女3
Ashlynn Brooke photograph of a group3
Ashlynn Brooke a group sexy photo1
男人帮杂志里的惹火性感美女2
为什么别人说你是疯子
A beautiful girl to bud2
Download software ranking
linux安装大全
终极变速大师Speeder3.26
SQL2000 For 4IN1
Dance with duck(male prostitution)
打鸟视频
Boxer Classic video2
Tram sex maniac 2 (H) rar bag17
Eclipse 4.2.2 For Win32
实战黑客不求人
jBuilder2006
归海一刀 published in(发表于) 2014/2/17 7:31:43 Edit(编辑)
PHP实例:PHP操作文件类_[PHP教程]

PHP实例:PHP操作文件类_[PHP教程]

PHP实例:PHP操作文件类_[PHP教程]

发一个刚写完的文件操作类


第一次写类,写的不好,大家多提意见,不过不要骂我
刚才又加了两个功能,又加了注释,高手一定帮我看看哪有问题啊,谢谢
file.class.php


/**
*本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
* 列出目录里的文件等功能,路径后面别忘了加"/"
*
* @author 路人郝
* @copyright myself
* @link [url=http://www.phpr.cn]www.phpr.cn[/url]
*
*/
class fileoperate
{
var path;// 文件路径
var name;//文件名
var result;//对文件操作后的结果

/**
* 本方法用来在path目录下创建name文件
*
* @param string path
* @param string name
*/
function creat_file(path,name)//建立文件
{
filename=path.name;
if (file_exists(filename))
{
echo "文件已经存在,请换个文件名";
}
else
{
if (file_exists(path))
{
touch(name);
rename(name,filename);
echo "文件建立成功
";
}
else{
echo "目录不存在,请检查";
}
}
}

/**
* 本方法用来写文件,向path路径下name文件写入content内容,bool为写入选项,值为1时
* 接着文件原内容下继续写入,值为2时写入后的文件只有本次content内容
*
* @param string_type path
* @param string_type name
* @param string_type content
* @param bool_type bool
*/
function write_file(path,name,content,bool) //写文件
{
filename=path.name;
if (bool==1) {
if (is_writable(filename)) {
handle=fopen(filename,'a');
if (!handle) {
echo "文件不能打开或文件不存在";
exit;
}
result=fwrite(handle,content);
if (!result) {
echo "文件写入失败";
}
echo "文件写入成功";
fclose(handle);
}
else echo "文件不存在";
}
if (bool==2) {
if (!file_exists(filename)) {
this->creat_file(path,name);
handle=fopen(filename,'a');
if (fwrite(handle,content));
echo "文件写入成功";

}
else {
unlink(filename);
this->creat_file(path,name);
this->write_file(path,name,content,1);
echo "文件修改成功";
}
}

}

/**
* 本方法删除path路径下name文件
*
* @param string_type path
* @param string_type name
*/
function del_file(path,name){ //删除文件
filename=path.name;

if (!file_exists(filename)) {
echo "文件不存在,请确认路径是否正确";
}
else {
if (unlink(filename)){
echo "文件删除成功";
}
else echo "文件删除失败";
}

}

/**
* 本方法用来修改path目录里name文件中的内容(可视)
*
* @param string_type path
* @param string_type name
*/
function modi_file(path,name){ //文件修改
filename=path.name;
handle=fopen(filename,'r+');
content=file_get_contents(filename);
echo "";
echo "文件内容";
echo "

";
echo "文件路径

";
echo "";
echo "";
}

/**
* 本方法用来复制name文件从spath到dpath
*
* @param string name
* @param string spath
* @param string dpath
*/
function copy_file(name,spath,dpath) //文件复制
{
filename=spath.name;
if (file_exists(filename)) {
handle=fopen(filename,'a');
copy(filename,dpath.name);
if (file_exists(dpath.name))
echo "文件复制成功";
else echo "文件复制失败";
}
else echo "文件不存在";
}

/**
* 本方法把name文件从spath移动到path路径
*
* @param string_type path
* @param string_type dirname
* @param string_type dpath
*/
function move_file(name,spath,dpath) //移动文件
{
filename=spath.name;
if (file_exists(filename)) {
result=rename(filename,dpath.name);
if (result==false or !file_exists(dpath))
echo "文件移动失败或目的目录不存在";
else
echo "文件移动成功";
}
else {
echo "文件不存在,无法移动";
}
}

/**
* 本方法把filename文件重命名为newname文件
*
* @param string_type filename
* @param string_type newname
*/
function rename_file(filename,newname) { //文件或目录更名
path=pathinfo(filename);
dir=path['dirname']; //得到文件路径
newfilename=dir.newname;
if (file_exists(filename)) { //判断文件是否存在
result=rename(filename,newfilename);
if (result==true)
echo "文件更名成功";
else
echo "文件更名失败";
}
else
echo "文件不存在";
}

/**
* 本方法用来列出目录里的文件或目录switch为1时按字母顺序列出所有目录和文件
* switch为2则只列出目录,switch为3时,只列出文件名
*
* @param string_type path
* @param int_type switch
*/
function list_filename(path,switch) //列出文件和目录名
{
if (file_exists(path)) {
dir=scandir(path);
if (switch==1){ //如果switch为1则按字母顺序列出所有目录和文件
for (i=0;i<=count(dir);i++)
{
if (dir[i]!="." and dir[i]!="..")
{
echo "dir[i]
";
}
}
}
if (switch==2) //switch为2则只列出目录
{
for (i=0;i<=count(dir);i++)
{
x=is_dir(path.dir[i]);
if (dir[i]!="." and dir[i]!=".." and x==true)
{
echo "dir[i]
";
}
}
}
if (switch==3) //switch为3时,只列出文件名
{
for (i=0;i<=count(dir);i++)
{
x=is_dir(path.dir[i]);
if (dir[i]!="." and dir[i]!=".." and x==false)
{
echo "dir[i]
";
}
}
}
}
}

/**
* 本方法在path目录下创建名为dirname的目录
*
* @param string_type path
* @param string_type dirname
*/
function creat_dir(path,dirname){ //创建目录
if (file_exists(path)) {
result=mkdir(path.dirname);
if (result)
echo "目录建立成功";
else
echo "目录建立失败";
}
else
echo "路径不存在,请重新输入";
}

/**
* 本方法删除pathname目录,包括该目录下所有的文件及子目录
*
* @param string_type pathname
*/
function del_dir(pathname){ //删除目录及目录里所有的文件夹和文件
if (!is_dir(pathname))
{exit("你输入的不是一个目录,请检查") ;}
handle=opendir(pathname);
while ((fileordir=readdir(handle)) !== false) {
if (fileordir!="." && fileordir!="..") {
is_dir("pathname/fileordir")?
this->del_dir("pathname/fileordir"):
unlink("pathname/fileordir");
}
}
if (readdir(handle) == false)
{
closedir(handle);
rmdir(pathname);
}
}
}
?>



modi_file.php

include('file.class.php');
content=_POST['content'];//得到文件内容
//echo content;//
filename=_POST['filename'];
path=pathinfo(filename); //得到文件路径
dir=path['dirname']."/"; //目录名
name=path['basename']; //文件名
op=new fileoperate();
op->write_file("dir","name",content,2);
?>

test.php

include('file.class.php');
op=new fileoperate();
//op->creat_file("./","aa3.txt");
//op->del_file("./","aa3.txt");
//b="fdfdsfdsfdsfdfdfdfdfdsfdsf1233456";
//op->write_file("./","aa3.txt",b,1);
//op->modi_file("./","aa3.txt");
//op->copy_file("aa3.txt","./","../a/");
//op->move_file("aa3.txt","../","./");
//op->rename_file("aa3.txt","aa2.txt");
//op->list_filename("../",3);
//op->creat_dir("/wwwroot","wesley");
//op->del_dir("/aaa/test");?>


来源:phpchina.com






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.