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

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

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

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

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

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

<?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 "文件不存在,无法移动";
}
}
}
?>

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

  1. java csv 文件 操作类

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

  2. Qt5:Qt文件操作类 QFile

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

  3. C# 文件操作类大全

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

  4. File 文件操作类 大全

    File  文件操作类  大全 许多人都会对文件操作感到很难  我也是  但是一个好的项目中必定会涉及到文件操作的 文件的复制 粘贴  等等等 公司大佬写了 一个文件操作的工具类 感觉还是棒棒的啦   ...

  5. php文件操作类

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

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

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

  7. 文件操作类CFile

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

  8. asp.net文件操作类

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

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

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

随机推荐

  1. Python 处理异常栈模块——traceback 模块

    异常捕捉 通常我们在项目中,针对异常的捕捉会使用 try + except,基本形式如下: try: # 主代码 except IndexError as e: # 索引异常时执行这里 logger. ...

  2. what's the psutil模块

    what's the psutil模块 psutil 是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要用来做系统监控,性能分析,进程管理.它实现 ...

  3. Multitenancy

    Multitenancy https://www.gartner.com/en/information-technology/glossary/multitenancy 公用一套物理环境,划分出不同的 ...

  4. 百度SMS发送短信C#

    /// <summary> /// 百度接口签名帮助类 /// </summary> public class BaiduApiHelper { #region 构造函数 // ...

  5. 【视频开发】Gstreamer中一些gst-launch常用命令

    GStreamer是著名的开源多媒体框架,功能强大,其命令行程序 gst-launch 可以实现很多常规测试.播放等,作为系统调试等是非常方便的. 1.摄像头测试 gst-launch v4l2src ...

  6. MF 模拟器读取PC串口数据

    using System; using Microsoft.SPOT; using Microsoft.SPOT.Input; using Microsoft.SPOT.Presentation; u ...

  7. django缓存相关

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_39726347/articl ...

  8. JS实现文字转语音播放

    JS实现文字转语音播放背景实现方式第一种:百度文字转语音开放API第二种:微软TTS语音引擎第三种:SpeechSynthesisUtterance总结背景在做项目的过程中,经常会遇到场景是客户要求播 ...

  9. java字符串截取

    import org.apache.commons.lang.StringUtils; public class substr{ public static void main(String[] ar ...

  10. 机器学习中什么是端到端的学习(end-to-end learning)?

    相对于深度学习,传统机器学习的流程往往由多个独立的模块组成,比如在一个典型的自然语言处理(Natural Language Processing)问题中,包括分词.词性标注.句法分析.语义分析等多个独 ...