工作中经常用php操作文件,因此把常用文件操作整理出来:

 class hylaz_file{
/**
* Read file
* @param string $pathname
* @return string content
*/
public static function read_file($pathname){
return @file_get_contents($pathname);
}
/**
* Write File
* @param string $pathname 文件名称
* @param string $data 写入到文件的数据
* @param string $md 打开文件模式
* @return int bool
*/
public static function write_file($pathname,$data,$md='wb'){
if(!$fp=fopen($pathname,$mode))
return false;
flock($fp,LOCK_EX);
for($result=$written=0,$length=strlen($data),$written<$length;$written+=$result){
if(($result=fwrite($fp,substr($data,$written)))===FALSE){
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
/**
* 获取文件信息
* @param string path of file
* @param array array or comma separated string of information returned
* @return array
*/
public static function file_info($file,$return_value=array('name', 'server_path', 'size', 'date')){
if(!file_exists($file)){
return false;
}
if(is_string($return_value)){
$return_value=explode(",",$return_value);
}
foreach($return_value as $key){
switch ($key){
case "name":
$fileinfo['name']=basename($path);
break;
case "server_path":
$fileinfo['server_path']=$file;
break;
case 'date':
$fileinfo['date']=filemtime($file);
break;
case 'size':
$fileinfo['size']=filesize($file);
break;
case 'readable':
$fileinfo['readable']=is_readable($file);
break;
case 'writable':
$fileinfo['writable']=is_writable($file);
break;
case 'executable':
$fileinfo['executable']=is_executable($file);
break;
case 'fileperms':
$fileinfo['fileperms']=fileperms($file);
break; }
}
return $fileinfo;
}
/**
* 获得八进制的权限位
* @param string $perms
* @return string
*/
public static function octal_permissions($perms){
return substr(sprintf("%o",$perms), -3);
}
}

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 面向 ...

随机推荐

  1. DP(悬线法)【P1169】 [ZJOI2007]棋盘制作

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p1169 棋盘制作 题目大意 给定一个01棋盘,求其中01交错的最大正方形与矩形. 解题思路: 动态规划---悬线法 以下内 ...

  2. Superbull(最大生成树)(Kruskal)

    Superbull 时间限制: 1 Sec  内存限制: 64 MB提交: 49  解决: 13[提交][状态][讨论版] 题目描述 Bessie and her friends are playin ...

  3. 正则 lazy

  4. python aiohttp sancio 框架性能测试

    开头先啰嗦两句: 由于本人有开发一个博客的打算,所以近期开始选型python的web框架重头学习,选了两款非常火的 aio web框架 aiohttp 和 sancio 进行性能测试以及开发喜好的调研 ...

  5. 取消Gridvie中button的焦点

    Gridview中添加button,onclick方法使得 GridView的setOnItemClickListener方法无效. 解决方法: 设置Button的XML布局文件,也就是自定义的Ada ...

  6. POJ 2068 Nim(博弈论)

    [题目链接] http://poj.org/problem?id=2068 [题目大意] 给出两队人,交叉放置围成一圈,每个人能取的石子数有个上限,各不相同 轮流取石头,取到最后一块石头的队伍算输,问 ...

  7. [BZOJ4538]网络

    今天打比赛,毒瘤yww把这题出到$n,m\leq 5\times10^5$,因为不会写整体二分所以来写坑爹的$O\left(n\log_2n\right)$做法 考虑按重要度建权值线段树(相同权值的请 ...

  8. 【二分答案】【Heap-Dijkstra】bzoj2709 [Violet 1]迷宫花园

    显然最短路长度随着v的变化是单调的,于是可以二分答案,据说spfa在网格图上表现较差. #include<cstdio> #include<cstring> #include& ...

  9. 【分类讨论】bzoj3856 Monster

    #include<cstdio> using namespace std; int T=0; long long h,a,b,k; int main() { freopen("b ...

  10. 【矩阵哈希】【哈希表】bzoj2351 [BeiJing2011]Matrix

    引用题解:http://blog.csdn.net/popoqqq/article/details/41084047 #include<cstdio> #include<cstrin ...