首页 技术 正文
技术 2022年11月22日
0 收藏 348 点赞 4,585 浏览 1569 个字

工作中经常用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);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866