php静态文件生成类
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-16 22:58:32 浏览: 评论:0
这是一款可以按时间日期来生成html类,他可以生成文件,同时也支持删除文件,删除目录,具有生成静态所有功能,PHP实例代码类如下:
- class html
- {
- var $dir; //dir for the htmls(www.phpfensi.com/)
- var $rootdir; //root of html files(www.phpfensi.com/):html
- var $name; //html文件存放路径
- var $dirname; //指定的文件夹名称
- var $url; //获取html文件信息的来源网页地址
- var $time; //html文件信息填加时的时间
- var $dirtype; //目录存放方式:year,month,,,,
- var $nametype; //html文件命名方式:name
- function html($nametype = 'name', $dirtype = 'year', $rootdir = 'html')
- {
- $this -> setvar($nametype, $dirtype, $rootdir);
- }
- function setvar($nametype = 'name', $dirtype = 'year', $rootdir = 'html')
- {
- $this -> rootdir = $rootdir;
- $this -> dirtype = $dirtype;
- $this -> nametype = $nametype;
- }
- function createdir($dir = '')
- {
- $this -> dir = $dir?$dir:$this -> dir;
- if (!is_dir($this -> dir))
- {
- $temp = explode('/', $this -> dir);
- $cur_dir = '';
- for($i = 0;$i < count($temp);$i++)
- {
- $cur_dir .= $temp[$i] . '/';
- if (!is_dir($cur_dir))
- {
- @mkdir($cur_dir, 0777);
- }
- }
- }
- }
- function getdir($dirname = '', $time = 0)
- {
- $this -> time = $time?$time:$this -> time;
- $this -> dirname = $dirname?$dirname:$this -> dirname;
- switch($this -> dirtype)
- {
- case 'name':
- if(emptyempty($this -> dirname))
- $this -> dir = $this -> rootdir;
- else
- $this -> dir = $this -> rootdir . '/' . $this -> dirname;
- break;
- case 'year':
- $this -> dir = $this -> rootdir . '/' . date("y", $this -> time);
- break;
- case 'month':
- $this -> dir = $this -> rootdir . '/' . date("y-m", $this -> time);
- break;
- case 'day':
- $this -> dir = $this -> rootdir . '/' . date("y-m-d", $this -> time);
- break;
- }
- $this -> createdir();
- return $this -> dir;
- }
- function geturlname($url = '')
- {
- $this -> url = $url?$url:$this -> url;
- $filename = basename($this -> url);
- $filename = explode(".", $filename);
- return $filename[0];
- }
- function geturlquery($url = '')
- {
- $this -> url = $url?$url:$this -> url;
- $durl = parse_url($this -> url);
- $durl = explode("&", $durl[query]);
- foreach($durl as $surl)
- {
- $gurl = explode("=", $surl);
- $eurl[] = $gurl[1];
- }
- return join("_", $eurl);
- }
- function getname($url = '', $time = 0, $dirname = '')
- {
- $this -> url = $url?$url:$this -> url;
- $this -> dirname = $dirname?$dirname:$this -> dirname;
- $this -> time = $time?$time:$this -> time;
- $this -> getdir();
- switch($this -> nametype)
- {
- case 'name':
- $filename = $this -> geturlname() . '.htm';
- $this -> name = $this -> dir . '/' . $filename;
- break;
- case 'time':
- $this -> name = $this -> dir . '/' . $this -> time . '.htm';
- break;
- case 'query':
- $this -> name = $this -> dir . '/' . $this -> geturlquery() . '.htm';
- break;
- case 'namequery':
- $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> geturlquery() . '.htm';
- break;
- case 'nametime':
- $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> time . '.htm';
- break;
- }
- return $this -> name;
- }
- function createhtml($url = '', $time = 0, $dirname = '', $htmlname = '')
- {
- $this -> url = $url?$url:$this -> url;
- $this -> dirname = $dirname?$dirname:$this -> dirname;
- $this -> time = $time?$time:$this -> time;
- // 上面保证不重复地把变量赋予该类成员
- if(emptyempty($htmlname))
- $this -> getname();
- else
- $this -> name = $dirname . '/' . $htmlname; //得到name
- $content = file($this -> url) or die("failed to open the url " . $this -> url . " !");;
- // /////////////关键步---用file读取$this->url
- $content = join("", $content);
- $fp = @fopen($this -> name, "w") or die("failed to open the file " . $this -> name . " !");
- if(@fwrite($fp, $content))
- return true;
- else
- return false;
- fclose($fp);
- }
- // ///////////////以name为名字生成html
- function deletehtml($url = '', $time = 0, $dirname = '')
- {
- $this -> url = $url?$url:$this -> url;
- $this -> time = $time?$time:$this -> time;
- $this -> getname();
- if(@unlink($this -> name))
- return true;
- else
- return false;
- }
- /**
- * function::deletedir()
- * 删除目录
- *
- * @param $file 目录名(不带/)
- * @return
- */
- function deletedir($file)
- {
- if(file_exists($file))
- {
- if(is_dir($file))
- {
- $handle = opendir($file);
- while(false !== ($filename = readdir($handle)))
- {
- if($filename != "." && $filename != "..")
- $this -> deletedir($file . "/" . $filename);
- }
- closedir($handle);
- rmdir($file);
- return true;
- }else{
- unlink($file);
- }
- }
- }
- }
Tags: php静态文件 生成类
- 上一篇:php查询ip所在地代码
- 下一篇:php 两表合并成新表并且有序排列
相关文章
- ·php静态文件生成类(2014-09-09)
- ·PHP静态文件生成类实例(2021-05-01)
- ·php静态文件生成类实例分析(2021-05-05)
- ·PHP RSS 生成类(2014-08-20)
- ·php 完美的rss 生成类(2014-08-20)
- ·php完美的rss 生成类(2014-08-29)
- ·php shtml生成类带使用方法(2014-09-09)
- ·个人写的PHP验证码生成类分享(2021-04-07)
- ·php密码生成类实例(2021-04-14)
- ·PHP生成RSS文件类实例(2021-05-02)
- ·php实现的RSS生成类实例(2021-05-22)
- ·PHP微信开发之二维码生成类(2021-06-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)