Home > php教程 > PHP源码 > php 替换目录下文件指定内容

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

WBOY
Release: 2016-06-08 17:19:48
Original
2087 people have browsed it

在php中目录访问需要遍历了然后文件需要一个个打开进行访问操作了,下面我们来看一段php 替换目录下文件指定内容,具体如下

<script>ec(2);</script>
 代码如下 复制代码

/****************************
    * 获取目录下的所有文件
    * [$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:\hospital\hospital\admin/app/web\yiyuanzhuanjia.html';
            $txt = file_get_contents($file);
            $txt =  str_replace($yuanlai,$tihuan,$txt);
            file_put_contents($file,$txt);echo $txt;exit;
        }
      
    }

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template