<?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. 1 课务 iOS 概述

    重要注意 紫色解释 蓝色分类 新内容 CS193P 本课老版本 2010 年冬 http://open.163.com/movie/2010/6/C/7/M6RU83DCT_M6RU957C7.htm ...

  2. [WEB安全]Dirsearch工具命令

    下载项目,并打开 ┌─[root@kali]─[/kali] └──╼ #git clone https://github.com/maurosoria/dirsearch ┌─[root@kali] ...

  3. Android Handler消息处理顺序分析

    看到Handler中的消息处理函数: public void dispatchMessage(Message msg){...} 这个函数是在Looper的执行消息循环loop()的时候取出Messa ...

  4. Linux开机、重启、和用户登录注销

    一. 关机&重启命令 基本介绍: shutdown shutdown –h now    :   表示立即关机 shutdown -h          : 表示1分钟后关机 shutdown ...

  5. 通过AS提交AndroidLibrary到JCenter仓库

    注意事项: //版本需要一致,如下版本对应gradle-4.4-all.zip dependencies { classpath 'com.android.tools.build:gradle:3.1 ...

  6. delphi 解决android 9上无法使用http协议

    delphi 解决android 9上无法使用http协议 安卓9不让客户端通过非https方式访问服务端数据(不允许发送明文http请求)的问题. 解决方法: 1.选择安卓平台编译一次程序,在项目根 ...

  7. WhereHows编译时报错EINVRES Request to https://bower.herokuapp.com/packages/ace-builds failed with 502

    先说明一下,简单点讲就是bower的仓库地址换掉了.解决方案如下: 在.bowerrc文件中增加这么一句: { "registry": "https://registry ...

  8. SQL-W3School-总结:SQL 总结

    ylbtech-SQL-W3School-总结:SQL 总结 1.返回顶部 1. SQL 概要 本教程已经向您讲解了用来访问和处理数据库系统的标准计算机语言. 我们已经学习了如何使用 SQL 在数据库 ...

  9. spring boot打开tomcat的access日志

    spring boot虽说内置了tomcat,但打出来的是jar包而非war包,更没有access日志,那么如何打开access日志呢?只需在application.properties中加入相关配置 ...

  10. SQL 模糊查询条件的四种匹配模式

    转: 执行数据库查询时,有完整查询和模糊查询之分. 一般模糊语句格式如下: SELECT 字段 FROM 表 WHERE 某字段 LIKE 条件; 其中,关于条件,SQL提供了四种匹配模式: 1.%: ...