zoukankan      html  css  js  c++  java
  • PHP生成静态文件

    本文引自网络

    首先引入自己的FILE类:

    <?php
    /**
    * 文件处理类
    */
    class Files
    {
    private $resource = null; //文件资源句柄
    function __construct($fileName,$mode='r')
    {
    $dirName = dirname($fileName);//文件路径
    $baseName = basename($fileName);//文件名

    //检查并创建文件夹
    self::mkdir($dirName);

    $this->resource = fopen($fileName,$mode.'b');
    if($this->resource)
    {
    flock($this->resource,LOCK_EX);//进行锁定
    }
    }
    //文件写入函数
    public function write($content)
    {
    $worldsnum = fwrite($this->resource,$content);
    return is_bool($worldsnum) ? false : $worldsnum;
    }
    }

    写一个函数来生成静态文件:

    private function writeHtml($path,$content){
    if(! class_exists('Files')){
    $this -> load ->file(APPPATH.'libraries/Files'.EXT);//本人用的CI 框架,引入文件类这里要稍作改动
    $f = new Files($path,'w+');
    $res = $f->write($content);
    $f->save();
    }

    最后在需要生成静态页面的地方调用上面那个函数:

    function crativehtml{
    $url = "PHP动态文件路径";
    $content = file_get_contents($url);//获取文件内容
    $this -> _writeFile($path.'文件名.html',$content);
    }

    就这样一个静态文件就生成了,最后一个文件可以写在多个地方来生成不同的静态文件、

  • 相关阅读:
    【python】requests库
    pycharm新建项目时选择virtualenv的说明
    cookie、session、token
    读写锁--DEMO
    锁降级--防止线程安全问题
    mysql-left join
    index-document-shard
    ES-常见搜索方式
    SpringBoot在自定义类中调用service层等Spring其他层
    mongodb crud
  • 原文地址:https://www.cnblogs.com/yszr/p/9321611.html
Copyright © 2011-2022 走看看