php 替换目录下文件指定内容

作者:袖梨 2022-06-24
 代码如下 复制代码

/****************************
    * 获取目录下的所有文件
    * [$dir] 文件夹路径
    ****************************/
    function deepScanDir($dir) {
        $fileArr = array ();
        $dirArr = array ();
        $dir = rtrim($dir, '//');
        if (is_dir($dir)) {
            $dirHandle = opendir($dir);
            while (false !== ($fileName = readdir($dirHandle))) {
                $subFile = $dir . DIRECTORY_SEPARATOR . $fileName;
                if (is_file($subFile)) {
                    $fileArr[] = $subFile;
                }
                elseif (is_dir($subFile) && str_replace('.', '', $fileName) != '') {
                    $dirArr[] = $subFile;
                    $arr = $this->deepScanDir($subFile);
                    $dirArr = array_merge($dirArr, $arr['dir']);
                    $fileArr = array_merge($fileArr, $arr['file']);
                }
            }
            closedir($dirHandle);
        }
        return array (
            'dir' => $dirArr,
            'file' => $fileArr
        );
    }

 

/*
    * 替换成APP中可用的路径,在web文件夹中
    */
    public function ok_web(){
        //查找字符
        $yuanlai = array(
            '"/resources/',
            '"/uploads/',
            '"/web/',
            'href="/"',
            '/web',
            'typedir+'/'+v.aid+"',
            'v.litpic',
        );
        //替换字符
        $tihuan = array(
            '"../resources/',
            '"../uploads/',
            '"',
            'href="../index.html"',
            '',
            'v.aid+"',
            '".."+v.litpic'
        );
        
        //查找的文件夹
        $dir = WEBROOT.'/app/web';
        //获取文件
        $dirs = $this->deepScanDir($dir);
        //文件字符串替换
        foreach($dirs['file'] as $file){
            $file = 'G:hospitalhospitaladmin/app/webyiyuanzhuanjia.html';
            $txt = file_get_contents($file);
            $txt =  str_replace($yuanlai,$tihuan,$txt);
            file_put_contents($file,$txt);echo $txt;exit;
        }
      
    }

相关文章

精彩推荐