2:考虑json文件有空格
/*
*time:2016年12月3日16:42:46
*author:zhangchennyang
*功能:json文件遍历压缩
*
*/
header('content-type:text/html;charset=utf8');
frename('../json'); //使用该方法参数为文件夹目录
function frename($dirname)
{
if (!is_dir($dirname)) {
echo {$dirname}不是一个有效的目录!;
exit();
}
$handle = opendir($dirname);
while (($fn = readdir($handle)) !== false) {
if ($fn != '.' && $fn != '..') {
// echo fn = . $fn . \n\r;
$curdir = $dirname . '/' . $fn;
// echo curdir = . $curdir . \r\n;
if (is_dir($curdir)) {
frename($curdir);
} else {
$path = pathinfo($curdir);
//var_dump($curdir);
$file_type=$path['extension'];
if($file_type=='json'){
$str=file_get_contents($curdir); //根据文件夹读取文本获得字符串
$arr=json_decode($str,true); //转为数组
$k_str=json_encode($arr);//数组转换为字符串
$statu=file_put_contents($curdir,$k_str);
if($statu){ //格式化成功
$time=date('y-m-d h:i:s'); //执行时间
$wenjian=$curdir;
$ok_= 'file' . $wenjian . '---time:' . $time.'-'.php_eol;
file_put_contents('ok.txt',$ok_, file_append);
}else{
$time=date('y-m-d h:i:s'); //执行时间
$wenjian=$curdir;
$error_info= 'file' . $wenjian . '---time:' . $time.'-'.php_eol;
file_put_contents('error.txt',$error_info, file_append);
}
}
}
}
}
}
//去掉空格等等字符串
/*function trimall($str){
$qian=array( , ,\t,\n,\r);
$hou=array(,,,,);
return str_replace($qian,$hou,$str);
}*/
