php处理文件生成新文件

<?php
require_once("Des.php");
ini_set('memory_limit', '-1');
$des = new Pay_Crypt_Des();
$key = '***********';
if(!empty($argv[1]) && !empty($argv[2])){
    $path = $argv[1]; // 原始文件目录
    $outputDir = $argv[2]; // 输出数据的文件夹 绝对路径
}else{
    echo "path or outputDir is empty";die;
}
function list_file($path, $des, $key, $outputDir){
    # 返回指定目录中的文件和目录的数组
    $temp = scandir($path);
    foreach($temp as $v){
        $item = $path.'/'.$v;
        if(is_dir($item)){ // 是文件夹
            if($v=='.' || $v=='..'){
               continue;
            }
            $sonDir = $outputDir.'/'.$v;
            if (!file_exists($sonDir)){
                mkdir($sonDir, 0777, true);
            }else{
                deldir($sonDir.'/');
            }
            list_file($item, $des, $key, $sonDir);
        }else{ // 是文件
            $f1 = fopen($item, 'r');
            $f2 = fopen($outputDir.'/'.$v, 'w');
            while (!feof($f1)){
                $line = fgets($f1);
                $arr = explode("\t", $line);
                if(isset($arr[7]) && preg_match("/^[0-9]{4}.*=[0-9]{4}$/", $arr[7])){
                    $arr[7] = decrypt_cardno($arr[7], $key, $des);
                    $line = implode("\t", $arr);
                }
                fputs($f2,$line);
            }
            fclose($f2);
            fclose($f1);
        }
    }
}

list_file($path, $des, $key, $outputDir);

# 解密
function decrypt_cardno($encrypt_card,$key,$des){
    if(is_numeric($encrypt_card) || empty($encrypt_card)){
        return $encrypt_card;
    }
    if (strpos($encrypt_card, '@') === 0) {
        return $encrypt_card;
    }
    $decrypt_cardno = $des->decrypt_cardno($encrypt_card,$key,4, 4, true);
    return $decrypt_cardno;
}

# 清空文件夹 删除指定目录下的文件和文件夹
function deldir($path){
    if(is_dir($path)){
        $p = scandir($path);
        foreach($p as $val){
            if($val !="." && $val !=".."){
                if(is_dir($path.$val)){
                    deldir($path.$val.'/');
                    @rmdir($path.$val.'/');
                }else{
                    unlink($path.$val);
                }
            }
        }
    }
}

?>
发布了31 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/sosemseo/article/details/104074771
今日推荐