<?php
/**
*本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
* 列出目录里的文件等功能,路径后面别忘了加"/"
*/
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 "文件建立成功 </br>";
}
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 "<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>";
} /**
* 本方法用来复制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 "文件不存在,无法移动";
}
}
}
?>

本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录,列出目录里的文件等功能,路径后面别忘了加"/"

创建指定路径下的指定文件

* @param string $path(需要包含文件名和后缀)

* @param boolean $over_write 是否覆盖文件

* @param int $time 设置时间。默认是当前系统时间

* @param int $atime 设置访问时间。默认是当前系统时间

php文件操作类的更多相关文章

  1. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  2. 文件操作类CFile

    CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...

  3. asp.net文件操作类

    /** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...

  4. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  5. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  6. java csv 文件 操作类

    一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...

  7. Qt5:Qt文件操作类 QFile

    在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...

  8. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

  9. Java文件操作类效率对比

    前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...

  10. JAVA文件操作类和文件夹的操作代码示例

    JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...

随机推荐

  1. P4163 [SCOI2007]排列——next_permutation

    P4163 [SCOI2007]排列 注意要排序: next_permutation prev_permutation #include<cstdio> #include<cstri ...

  2. Scrapy不同的item指定不同的Pipeline

    scrapy不同的item指定不同的Pipeline from items import AspiderItem, BspiderItem, CspiderItem class myspiderPip ...

  3. 在本机上用IO流实现复制粘贴功能

    /** * 复制文件夹 * @param sourcePath * @param targetPath * @throws IOException */ public void copyFolder( ...

  4. Hbase 错误记录分析(1) region超时问题

    错误现象: 默认等待时间是60秒,超过这个时间就报超时问题了.因此需调整超时时间,默认为60秒,在配置文件 hbase-site.xml中: 调整成10分钟 <property>    & ...

  5. NOIP1999提高组 题解报告

    T1 导弹拦截 题目大意:依次有\(n\) (\(n \le 10^5\))枚导弹,一套导弹拦截系统只能拦截一系列高度递减的导弹(一套系统拦截的弹道不一定相邻).求一套系统最多能拦截多少导弹,以及最少 ...

  6. Buuctf pwn1 详细wp

    目录 程序基本信息 程序溢出点 确定返回地址 编写exp脚本 成功getshell 程序基本信息 我们可以看到这是一个64程序,没有保护开启. 程序溢出点 gets函数可以读取无限字符,存在栈溢出. ...

  7. mac上运行shell脚本遇到回车字符错误

    今天运行一段其他人给的shell脚本,遇到如下问题,这个脚本的内容如下: dname=\((dirname "\)PWD") mkdir ${dname}"/rom_pu ...

  8. Mac升级Node.js和npm到最新版本指令

    一.查看本机当前Node.js和npm版本 node -v npm -v 二.清除node.js的cache sudo npm cache clean -f 三.安装"n"版本管理 ...

  9. Arduino---HC-05 蓝牙模块

    蓝牙基础知识回顾: (一)Arduino和HC-05连接 注意:Arduino通过TX与HC-05进行通信,而Arduino的电压为5V,HC-05的允许电压为3.3V.短时间通信无妨(长时间可能烧毁 ...

  10. 【微信小程序】wx.navigateBack() 携带参数返回

    第一个页面: go_pick_time:function(e){ var that = this; var type = e.currentTarget.dataset.type; wx.naviga ...