欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【PHP实例:PHP操作文件类】,下面是详细的分享!
PHP实例:PHP操作文件类
<?php
class fileoperate
{
var path;// 文件路径
var name;//文件名
var result;//对文件操作后的结果
function creat_file(path,name)//建立文件
{
filename=path.name;
if (file_exists(filename))
{
echo "文件已经存在,请换个文件名";
}
else
{
if (file_exists(path))
{
touch(name);
rename(name,filename);
echo "文件建立成功 </br>";
}
else{
echo "目录不存在,请检查";
}
}
}
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 "文件修改成功";
}
}
}
function del_file(path,name){ //删除文件
filename=path.name;
if (!file_exists(filename)) {
echo "文件不存在,请确认路径是否正确";
}
else {
if (unlink(filename)){
echo "文件删除成功";
}
else echo "文件删除失败";
}
}
function modi_file(path,name){ //文件修改
filename=path.name;
handle=fopen(filename,'r+');
content=file_get_contents(filename);
echo "<form id='form1' name='form1' action='modi_file.php' method='post'>";
echo "<textarea name=content rows='10'>content</textarea>文件内容";
echo "<p>";
echo "<input type='text' name='filename' value=filename />文件路径<p>";
echo "<input name=ss type=submit value=修改文件内容 />";
echo "</form>";
}
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 "文件不存在";
}
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 "文件不存在,无法移动";
}
}
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 "文件不存在";
}
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]<br>";
}
}
}
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]<br>";
}
}
}
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]<br>";
}
}
}
}
}
function creat_dir(path,dirname){ //创建目录
if (file_exists(path)) {
result=mkdir(path.dirname);
if (result)
echo "目录建立成功";
else
echo "目录建立失败";
}
else
echo "路径不存在,请重新输入";
}
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
<?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
<?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");?>
以上所分享的是关于PHP实例:PHP操作文件类,下面是编辑为你推荐的有价值的用户互动:
相关问题:用php代码实现 文件操作类题目!!
答:get_navigation(){......} 内,query之前 加一句 global $db; 或者改为 function get_navigation($db){......} $navigation = get_navigation($db); >>详细
相关问题:php类实例化
答:给你举个例子吧: 比如说:现在有user.class.php mysql.class.php 现在用户类有注册、登录等一系列的操作(要使用数据库类) 我们直接在user.class.php 中把数据库类给实力化了 这样就了可以达到你的那个效果了; 千万不要在将所有的方法放在一... >>详细
相关问题:php实例化多个类
答:file1.php类如下: class A{ ... } file2.php调用file2.php中的类如下: include "file1.php"; class B{ $C = new A(); ..... } >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
