生成html页面我们需要使用到的文件系统操作函数包括有fopen,fread,filesize,fwrite,fclose了,这些是基本要用到了,还像删除,创建目录之类的,下面我们来看看.
1.PHP部分文件操作函数。(fopen , fread , filesize , fwrite , fclose)
2.unlink() , mkdir() 删除函数。
1.PHP部分文件操作函数
(1)fopen 打开文件函数。 R / W / A
格式:fonpen(路径和文件名,打开方式);
(2)fread 读取文件内容。
格式:fread(打开的文件,结束的位置);
(3)filesize 读取文件大小,字节为计量单位。
格式:filesize(路径和文件名);
(4)fwrite 写入文件内容。
格式:fwrite(路径和文件名,写入的内容);
(5)fclose 关闭打开的文件。
格式:fclose(路径和文件名);
2.unlink(); mkdir(); 删除函数
unlink(); 删除文件函数
格式:unlink(路径和文件);
mkdir(); 删除目录函数
格式:mkdir(路径和目录名);
实例操作,代码如下:
- <?php
- $title = "新标题";
- $content = "新内容www.phpfensi.com";
- $fp = fopen("tmp.htm", "r"); //打开文件,以只读方式。
- $str = fread($fp, filesize("tmp.htm")); //读取文件内容,格式:fread(打开的文件,结束的位置);。
- $str = str_replace("{title}", $title, $str); //将str变量中的路径文件内容替换掉重新赋值
- $str = str_replace("{content}", $content, $str);
- fclose($fp); //以上为替换模板的内容。
- $id = "hello";
- $path = $id . '.htm';
- $handle = fopen($path, "w"); //写入方式打开新闻路径
- fwrite($handle, $str); //把刚才替换的内容写进生成的HTML文件
- fclose($handle);
- echo "生成成功";
- ?>
例,找到一个html生成类,代码如下:
- <?php
- // ————————————————————————–
- // File name : html.class.php
- // Description : www.phpfensi.com生成静态页面的类
- // Requirement : PHP5
- //
- // Copyright(C), 蟋蟀, 2013, All Rights Reserved.
- //————————————————————————–
- class myHtml{
- //生成html文件路径
- private $html_dir="./";
- //html文件名称
- private $html_name;
- //生成html文件的位置名称
- public $path;
- //缓存区内容
- private $content;
- //文件句柄
- private $handle;
- //内存指针
- private $accesses;
- //构造函数
- public function __construct($html_dir="",$html_name="")
- {
- $this->accesses++;
- //如果文件路径不存在建立文件夹
- if(opendir($html_dir)==0)
- {
- mkdir($html_dir);
- }
- $this->html_dir=$html_dir!=""?$html_dir:"./";
- $this->html_name=$html_name!=""?$html_name:substr(basename(__FILE__),0,strrpos(basename(__FILE__),".")).".html";
- $this->path= ($this->html_dir{strlen($this->html_dir)-1}=="/")
- ?($this->html_dir.$this->html_name):($this->html_dir."/".$this->html_name);
- ob_start();
- }
- //析构函数
- public function __destruct()
- {
- $this->accesses–;
- ob_end_clean();
- }
- //生成html页面
- function tohtml()
- {
- $this->content=ob_get_contents();
- if (is_file ($this->path)){
- @unlink ($this->path);
- }
- $handle = fopen ($this->path,"w");
- if (!is_writable ($this->path)){
- return false;
- }
- if (!fwrite ($handle,$this->content)){
- return false;
- }
- fclose ($handle); //关闭指针
- return $this->path;
- }
- }
- /*
- $html=new myHtml("./","z.htm");
- print "静态页面程序";
- $html->tohtml();
- */
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php生成HTML文件的应用和原理笔记
波比源码 » php生成HTML文件的应用和原理笔记
levofloxacin price order generic levofloxacin 500mg
buy generic anastrozole 1 mg boots viagra supplier cheap sildenafil for sale
doxycycline 100mg tablet vardenafil 20mg us purchase furosemide for sale
cost lopressor 100mg buy levitra 10mg generic order vardenafil 20mg pills
buy levitra 10mg online buy levitra pills medrol 8 mg online
order diflucan 200mg online cheap oral sildenafil us viagra
keflex 250mg without prescription order cephalexin 500mg online cheap purchase erythromycin pill
order viagra 50mg purchase viagra online cheap order cialis 40mg without prescription
zithromax oral neurontin 800mg drug gabapentin ca
lasix 40mg uk buy plaquenil 400mg pills plaquenil 200mg over the counter
generic sildenafil 100mg order tamoxifen 10mg online cheap buy rhinocort for sale
order tiotropium bromide 9 mcg online cheap order tiotropium bromide for sale terazosin 5mg cost
oral montelukast 10mg sildenafil 50mg cost sildenafil 100mg for sale
free casino poker online game free online blackjack
blackjack online free most reliable canadian online pharmacy writing a paper
assignment website arava 20mg cost order azulfidine 500mg for sale
olmesartan price divalproex cheap buy divalproex generic
buy carvedilol 25mg ditropan 2.5mg over the counter elavil over the counter
cheap amoxicillin for sale buy amoxil 1000mg ivermectin order online
purchase fosamax pill order ibuprofen 600mg sale order ibuprofen 600mg online
buy priligy 90mg sale buy domperidone pills domperidone over the counter
order requip pill buy rocaltrol sale oral trandate
tadalafil 20mg pills buy tadacip 20mg generic buy amoxicillin 250mg generic
order minocycline 50mg pill buy hytrin online hytrin medication
glycomet 1000mg brand purchase metformin pill order nolvadex 10mg pills
ivermectin buy uk order prednisone generic prednisone usa
zofran 8mg for sale order ondansetron 4mg for sale bactrim 480mg without prescription
accutane us order accutane 20mg zithromax pills
best antihistamine for itchy skin clavulanate canada order augmentin generic
prednisolone 10mg cost gabapentin online buy purchase lasix