php文件操作相关 date("Y-m-d H:i:s",fileatime($filename)), 'mtime'=date("y-m-d h:i:s",fileatime($filename)), 'ctime'=date("y-m-d h:i:s",fileatime($filename)), 'size'=trans_byte(filesize($filename)), 'type'=filetype($filename) ];}/** * 字
date("Y-m-d H:i:s",fileatime($filename)),
'mtime'=>date("y-m-d h:i:s",fileatime($filename)),
'ctime'=>date("y-m-d h:i:s",fileatime($filename)),
'size'=>trans_byte(filesize($filename)),
'type'=>filetype($filename)
];
}
/**
* 字节单位转换函数
* @param int $byte 字节数(b)
* @param integer $precision 默认精度
* @return string 转换后的字符串
*/
function trans_byte($byte,$precision=2){
$k=1024;
$Kb=$k;
$Mb=$Kb*$k;
$Gb=$Mb*$k;
$Tb=$Gb*$k;
if($byte<$Kb){
return $byte.'b';
}else if($byte<$Mb){
return round($byte/$Kb,$precision).'Kb';
}else if($byte<$Gb){
return round($byte/$Mb,$precision).'Mb';
}else if($byte<$Tb){
return round($byte/$Gb,$precision).'Gb';
}else{
return round($byte/$Tb,$precision).'Tb';
}
}
// var_dump(get_file_info('hello.txt'));
/**
* 文件读取函数(返回字符串)
* @param [type] $filename 文件名
* @return mixed 文件内容(string)|false
*/
function read_file($filename){
if(is_file($filename) && is_readable($filename)){//是文件并且可读
return file_get_contents($filename);
}
return false;
}
//var_dump(read_file('hello.txt'));
/**
* 文件读取函数(返回数组)
* @param [type] $filename 文件名
* @param boolean $skip_empty_lines 是否去除空行,默认不去除
* @return mixed array|false
*/
function read_file_array($filename,$skip_empty_lines=false){
if(is_file($filename)&&is_readable($filename)){
if($skip_empty_lines){
return file($filename,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
}else {
return file($filename);
}
return false;
}
}
//var_dump(read_file_array('hello.txt'));
//write_file('a/hello.txt',['a','b','c']);
/**
* 文件写入函数(默认为追加模式)
* @param [type] $filename 文件名
* @param [type] $date 数据
* @param boolean $clearFlag 追加覆盖标记,默认追加
* @return boolean true|false
*/
function write_file($filename,$date,$clearFlag=false){
//先判断目标路径是非存在
$oldDate=null;
$dirname=dirname($filename);
if(!file_exists($dirname)){
mkdir($dirname,0777,true);
}
//判断追加覆盖行为
if(!$clearFlag){
//检测文件是否存在并且可读
if(is_file($filename)&&is_readable($filename)){
//读取文件内容,并拼装新的内容
if(filesize($filename)>0){
$oldDate=file_get_contents($filename);
}
}
}
//判断写入内容是否为数组
if(is_array($date)||is_object($date)){
$date=serialize($date);//数组和对象先序列化
}
$date=$oldDate.$date."\r\n";
if(file_put_contents($filename,$date)){
echo '文件写入成功';
return true;
}else{
echo '文件写入失败';
return false;
}
}
/**
* 文件截断函数
* @param [type] $filename 文件名
* @param int $length 截取长度
* @return boolean true|false
*/
function truncate_file($filename,$length){
$length=$length<0?0:$length;
if(is_file($filename)&&is_readable($filename)){
$handle=fopen($filename,'r+');
ftruncate($handle,$length);
fclose($handle);
echo '文件截取成功';
return true;
}
echo '文件截取失败';
return false;
}
/**
* 文件下载函数(小文件)
* @param [type] $filename 文件名
* @param array $allowDownExt 允许下载格式
* @return void
*/
function down_file($filename,$allowDownExt=array('jpeg','jpg','png','gif','txt','html','php','rar','zip')){
//检测文件是否可读
if(!is_file($filename)||!is_readable($filename)){
echo '文件下载失败';
return false;
}
//检测文件是否允许下载
$ext=strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if(!in_array($ext,$allowDownExt)){
echo '不支持下载该格式文件';
return false;
}
//告诉浏览器输出的是字节流
header('Content-type:application/octet-stream');
//告诉浏览器返回的文件大小按字节进行计算
header('Accept-Ranges: bytes');
//告诉浏览器返回文件的大小
header('Accept-Length: '.filesize($filename));
//告诉浏览器文件作为附件处理,告诉浏览器最终下载完的文件名称
header('Content-Disposition:attachment;filename='.basename($filename));
//读取文件中的内容
readfile($filename);
exit;
}
/**
* 大文件下载函数
* @param [type] $filename 文件名
* @param array $allowDownExt 允许下载格式
* @return void
*/
function down_bigfile($filename,$allowDownExt=array('jpeg','jpg','png','gif','txt','html','php','rar','zip')){
//检测文件是否可读
if(!is_file($filename)||!is_readable($filename)){
echo '文件下载失败';
return false;
}
//检测文件是否允许下载
$ext=strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if(!in_array($ext,$allowDownExt)){
echo '不支持下载该格式文件';
return false;
}
//告诉浏览器输出的是字节流
header('Content-type:application/octet-stream');
//告诉浏览器返回的文件大小按字节进行计算
header('Accept-Ranges: bytes');
$filesize=filesize($filename);
//告诉浏览器返回文件的大小
header('Accept-Length: '.$filesize);
//告诉浏览器文件作为附件处理,告诉浏览器最终下载完的文件名称
header('Content-Disposition:attachment;filename='.basename($filename));
//读取文件中的内容
//readfile($filename);
$read_buffer=1024;
$sum_buffer=0;
$handle=fopen($filename,'rb');
while(!feof($handle) && $sum_buffer<$filesize){
echo fread($handle,$read_buffer);
$sum_buffer+=$read_buffer;
}
fclose($handle);
exit;
}
/**
* 单文件上传函数
* @param array $fileInfo 文件信息
* @param string $uploadPath 文件上传指定路径(默认upload)
* @param boolean $imageFlag 真实图片检测标志
* @param array $allowExt 允许上传文件类型数组
* @return mixed 服务器文件路径|错误信息(false)
*/
function upload_file($fileInfo,$uploadPath='./upload',$imageFlag=true,
$allowExt=array('jpeg','jpg','png','gif'),$max_File_Size=2097152){
//php7新增常量数组声明
//错误信息匹配数组
define('UPLOAD_ERRS',[
'upload_max_filesize'=>'上传文件超过upload_max_filesize的值',
'form_max_size'=>'上传文件超过表单max_file_size的值',
'upload_file_partial'=>'文件部分被上传',
'no_upload_file_select'=>'没有选择上传文件',
'upload_system_error'=>'系统错误',
'no_allow_ext'=>'非法文件类型',
'exceed_file_max_size'=>'上传的文件超过规定大小',
'not_true_image'=>'文件不是真实图片',
'not_http_post'=>'文件不是通过http post方式上传',
'file_move_error'=>'文件移动失败'
]);
if($fileInfo['error']==UPLOAD_ERR_OK){
$ext=strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
//取得文件扩展名然后校验
if(!in_array($ext,$allowExt)){
echo UPLOAD_ERRS['no_allow_ext'];
return false;
}
//检测上传文件大小
if($fileInfo['size']>$max_File_Size){
echo UPLOAD_ERRS['exceed_file_max_size'];
return false;
}
//检测是否为真实图片(可选选项)
if($imageFlag){
if(@!getimagesize($fileInfo['tmp_name'])){
echo UPLOAD_ERRS['not_true_image'];
return false;
}
}
//检测文件是否通过http post方式上传上来的
if(!is_uploaded_file($fileInfo['tmp_name'])){
echo UPLOAD_ERRS['not_http_post'];
return false;
}
//通过检测,现在开始移动服务器端临时文件
if(!is_dir($uploadPath)){//若指定路径不存在,系统自动创建
mkdir($uploadPath,0777,true);
}
//生成唯一文件名防止同名覆盖
$uniName=md5(uniqid(microtime(true),true)).'.'.$ext;
$dest=$uploadPath.DIRECTORY_SEPARATOR.$uniName;
if(@!move_uploaded_file($fileInfo['tmp_name'],$dest)){
return UPLOAD_ERRS['file_move_error'];
}
echo '文件上传成功','';
return $dest;
}else {//错误匹配
switch($fileInfo['error']){
case 1:
$mes=UPLOAD_ERRS['upload_max_filesize'];
break;
case 2:
$mes=UPLOAD_ERRS['form_max_size'];
break;
case 3:
$mes=UPLOAD_ERRS['upload_file_partial'];
break;
case 4:
$mes=UPLOAD_ERRS['no_upload_file_select'];
break;
case 6:
case 7:
case 8:
$mes=UPLOAD_ERRS['upload_system_error'];
break;
}
echo $mes;
return false;
}
}
