Total Pageviews

Thursday, 8 December 2011

把动态网页转为静态网页的PHP代码

把以下代码保存为html.php:
<html><head><title>把动态网页转为静态网页</title></head>
<body>
<form enctype='multipart/form-data' action="html.php" method="post">
动态网页地址:<input type="text" size="32" name="url" value="" /><br />
生成静态网页:<input type="text" size="12" name="file" value="" /><select name=type><option
value=".htm">.htm</option><option value=".html" selected>.html</option></select>
<input type="hidden" name="action" value="html" />
<input type=submit value='确定'>
</form>
<?php
if($_POST['action']=='html'){
                    if($_POST['file']=="" || !ereg("^[a-zA-Z0-9+]*$",$_POST['file'])){
                                        echo "<font color='red'>错误!文件名只能包含英文和数字!</font>";
                                        exit;
                    }
                    $content = @file_get_contents($_POST['url']);
                    if(empty($content)){
                                        echo "<font color='red'>错误!无法获取该网页内容!</font>";
                                        exit;
                    }
                    $filename = "html/".$_POST['file'].$_POST['type'];
                    if(!is_dir("html")) mkdir("html",0777);

                    $fp = fopen($filename,"w+");
                    $content = "<base href=\"".$_POST['url']."\">".$content;
                    fwrite($fp,$content);
                    fclose($fp);
                    echo "察看静态网页:<a href='".$filename."' target='_blank'>".$_POST

['file'].$_POST['type']."</a>";
}
?>
</body>
</html>

No comments:

Post a Comment