class fileInit {

     /**
* 创建空文件
* @param string $filename 需要创建的文件
* @return
*/
public function create_file($filename) {
if (file_exists($filename)) return false;
$this->create_dir(dirname($filename)); //创建目录
return @file_put_contents($filename,'');
} /**
* 写文件
* @param string $filename 文件名称
* @param string $content 写入文件的内容
* @param string $type 类型,1=清空文件内容,写入新内容,2=再内容后街上新内容
* @return
*/
public function write_file($filename, $content, $type = 1) {
if ($type == 1) {
if (file_exists($filename)) $this->del_file($filename); //删除文件
$this->create_file($filename);
return $this->write_file($filename, $content, 2);
} else {
if (!is_writable($filename)) return false;
$handle = @fopen($filename, 'a');
if (!$handle) return false;
$result = @fwrite($handle, $content);
if (!$result) return false;
@fclose($handle);
return true;
}
} /**
* 拷贝一个新文件
* @param string $filename 文件名称
* @param string $newfilename 新文件名称
* @return
*/
public function copy_file($filename, $newfilename) {
if (!file_exists($filename) || !is_writable($filename)) return false;
$this->create_dir(dirname($newfilename)); //创建目录
return @copy($filename, $newfilename);
} /**
* 移动文件
* @param string $filename 文件名称
* @param string $newfilename 新文件名称
* @return
*/
public function move_file($filename, $newfilename) {
if (!file_exists($filename) || !is_writable($filename)) return false;
$this->create_dir(dirname($newfilename)); //创建目录
return @rename($filename, $newfilename);
} /**
* 删除文件
* @param string $filename 文件名称
* @return bool
*/
public function del_file($filename) {
if (!file_exists($filename) || !is_writable($filename)) return true;
return @unlink($filename);
} /**
* 获取文件信息
* @param string $filename 文件名称
* @return array('上次访问时间','inode 修改时间','取得文件修改时间','大小','类型')
*/
public function get_file_info($filename) {
if (!file_exists($filename)) return false;
return array(
'atime' => date("Y-m-d H:i:s", fileatime($filename)),
'ctime' => date("Y-m-d H:i:s", filectime($filename)),
'mtime' => date("Y-m-d H:i:s", filemtime($filename)),
'size' => filesize($filename),
'type' => filetype($filename)
);
} /**
* 创建目录
* @param string $path 目录
* @return
*/
public function create_dir($path) {
if (is_dir($path)) return false;
fileInit::create_dir(dirname($path));
@mkdir($path);
@chmod($path, 0777);
return true;
} /**
* 删除目录
* @param string $path 目录
* @return
*/
public function del_dir($path) {
$succeed = true;
if(file_exists($path)){
$objDir = opendir($path);
while(false !== ($fileName = readdir($objDir))){
if(($fileName != '.') && ($fileName != '..')){
chmod("$path/$fileName", 0777);
if(!is_dir("$path/$fileName")){
if(!@unlink("$path/$fileName")){
$succeed = false;
break;
}
}
else{
self::del_dir("$path/$fileName");
}
}
}
if(!readdir($objDir)){
@closedir($objDir);
if(!@rmdir($path)){
$succeed = false;
}
}
}
return $succeed;
}
}

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. 对获取config文件的appSettings节点简单封装

    转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...

  2. html 基础之 <link>标签

    实例 链接一个外部样式表: <head> <link rel="stylesheet" type="text/css" href=" ...

  3. vs2012配置opencv及简单测试

    为visual studio2012搭建openCV平台,实现打开图片. 实现步骤: 1.1.配置环境变量 基于win7操作系统的环境配置步骤: 1.1.1 计算机—>属性—>更改设置—& ...

  4. avalon中常用的事件

     ms-on-change 相当于失去焦点事件. ms-on-input 相当于watch事件 http://www.runoob.com/jsref/event-oninput.html

  5. Mysql 记录

    1.创建用户命令: <!---->mysql> CREATE USER yy IDENTIFIED BY '123'; yy表示你要建立的用户名,后面的123表示密码 上面建立的用户 ...

  6. jQuery.access源码分析

    基本理解 jQuery.attr是jQuery.attr,jQuery.prop,jQuery.css提供底层支持,jQuery里一个比较有特色的地方就是函数的重载, 比如attr,有如下几种重载 $ ...

  7. RecyclerView实现ViewPager效果

    RecyclerView实现ViewPager效果,以及横向的ListView效果.效果图如下: Github: https://github.com/hpu-spring87/recyclervie ...

  8. Yii 多表关联relations,需要与with()方法联合使用

    1,首先多表关联是在models/xx.php的relations里配置的.而且是互配,但有区别. 格式: 'VarName'=>array('RelationType', 'ClassName ...

  9. microwindows Win32 API demo

    初次使用microwindows,资料有限,我也是费了很多功夫才明白.所以记录下来,好帮助那些爱学习的童鞋,另外请大虾们多多指教. 什么是microwindows,什么作用,等背景介绍我就不多说了,因 ...

  10. 类与对象 - PHP手册笔记

    基本概念 PHP对待对象的方式与引用和句柄相同,即每个变量都持有对象的引用,而不是整个对象的拷贝. 当创建新对象时,该对象总是被赋值,除非该对象定义了构造函数并且在出错时抛出了一个异常.类应在被实例化 ...