php 静态文件生成类

 更新时间:2016年11月25日 16:28  点击:1714

php教程 静态文件生成类
defined('php教程ox') or die(header("http/1.1 403 not forbidden"));           
          
class include_createstatic            
{           
               
    private $htmlpath = '';           
    private $path = '';           
    public $monthpath = '';           
    private $listpath = '';           
    private $content = '';           
    private $filename = '';           
    private $extname = '.html';           
               
    public function createhtml($type,$desname,$content)           
    {           
        $this->htmlpath = getappinf('htmlpath');           
        if (!file_exists($this->htmlpath))           
        {           
            @mkdir($this->htmlpath);           
        }           
        $this->path = $this->htmlpath.$this->monthpath.'/';           
        if (!file_exists($this->path))           
        {           
            @mkdir($this->path);           
        }           
        $this->listpath = $this->htmlpath.'list/';           
        if (!file_exists($this->listpath))           
        {           
            @mkdir($this->listpath);           
        }           
        switch ($type)           
        {           
            case 'index':           
                $this->filename = $desname;           
                break;           
            case 'list':           
                $this->filename = $this->listpath.$desname;           
                break;           
            case 'view':           
                $this->filename = $this->path.$desname;           
                break;           
        }           
        $this->filename .= $this->extname;           
        $this->content = $content;           
    }           
               
    public function write()           
    {           
        $fp=fopen($this->filename,'wb');           
        if (!is_writable($this->filename))           
        {           
            return false;           
        }           
        if (!fwrite($fp,$this->content))           
        {           
            return false;           
        }           
        fclose($fp);           
        return $this->filename;           
    }           
}      

//方法二

if(file_exists("./index.htm"))//看静态index.htm文件是否存在
{
$time=time();
//文件修改时间和现在时间相差?的话,直接导向htm文件,否则重新生成htm
if(time-filemtime("./index.htm")< 600)
{
header("location:classhtml/main.htm");
}
}
//在你的开始处加入ob_start();
ob_start();
//首页内容,就是你的动态部分了
//在结尾加入ob_end_clean(),并把本页输出到一个变量中
$temp=ob_get_contents();
ob_end_clean();
//写入文件
$fp=fopen("./index.htm",'w');
fwrite(fp,temp) or die('写文件错误');
//echo"生成html完成!";

 代码如下 复制代码

function my_file_get_contents($url, $timeout=30) {
 if ( function_exists('curl_init') ) 
 {
  $ch = curl_init();
  curl_setopt ($ch, curlopt_url, $url);
  curl_setopt ($ch, curlopt_returntransfer, 1);
  curl_setopt ($ch, curlopt_connecttimeout, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
 }
 else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )   
 {
  $file_contents = @file_get_contents($url);
 }
 else
 {
  $file_contents = '';
 }
 return $file_contents;
}


 

 代码如下 复制代码

function get_remote($body,$title){

 $img_array = array(); 
 $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址
 //die($img_path);
 $img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址
 $body = stripslashes(strtolower($body)); 
 preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array); 
 $img_array = array_unique($img_array[2]); 
 foreach ($img_array as $key => $value) {
  $get_file = my_file_get_contents($value,60);
  $filetime = time();   
  $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
  if(empty($get_file)){
   @sleep(10);
   $get_file = my_file_get_contents($value,30);
   if(empty($get_file)){
    $body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
    continue;
    }
  }
  if(!empty($get_file) ){
   if( mkdirs($img_path) )
   {
    $fp = fopen($img_path.$filename,"w");
    if(fwrite($fp,$get_file)){         
     $body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);
    }
    fclose($fp);
    @sleep(6);
   }   
  }    
 
 }
 $body =str_replace("<img","<img ",$body); 
 return $body;
 
}

function mkdirs($dir)
{
 if(!is_dir($dir)){
  if(!mkdirs(dirname($dir))){
   return false;}
  if(!mkdir($dir,0777)){
   return false;}
 }
 return true;
}

$str ='fasfsdafsa<img src=http://filesimg.111cn.net/2010/03/2010062300391582.jpg />';
echo get_remote($str,'图片');

本站原创教程转请注明来源,文件上传代码会这样做。

实现篇
一般情况,用php教程实现上传进度条就下面两种方法:
1.apc扩展(作者是php教程的创始人,5.2后php已经加入apc扩展)
2.pecl扩展模块 uploadprogress
不论是apc还是uploadprogress,都需要编译源码教程,因为原有的php函数根本不可能读取到临时文件夹里的东西。下面来看如何使用以及关键的代码:apc实现方法:
1.安装apc
2.配置php.ini,设置参数 apc.rfc1867=1
3.关键代码:

 代码如下 复制代码

if ($_server['request_method'] == ‘post’) {  //上传请求
$status = apc_fetch(’upload_’ . $_post['apc_upload_progress']);
$status['done'] = 1;
echo json_encode($status);  //输出给用户端页面里的ajax调用,相关文档请自己寻找
exit;
} elseif (isset($_get['progress_key'])) {   //读取上传进度
$status = apc_fetch(’upload_’.$_get['progress_key']);
echo json_encode($status);
exit;
}

uploadprogress实现方法:
1.使用pecl 安装uploadprogress
2.php.ini里面设置 uploadprogress.file.filename_template = “/tmp/upd_%s.txt”
3.关键代码:

 代码如下 复制代码
if($_server['request_method']==’post’) {
if (is_uploaded_file($_files['upfile']['tmp_name'])) {
$upload_dir = ‘your_path/’;
$ext        = strrchr($_files['video']['name'], ‘.’);
$sessid     = $_post['upload_identifier'] ;
$tmpfile    = $upload_dir . $sessid;
$sessfile   = $upload_dir . $sessid .$ext;
if (move_uploaded_file($_files['upfile']['tmp_name'],$tmpfile)) {
//上传成功
}
}

 

提供一款简单实用的php图片文件上传实例代码

 代码如下 复制代码

<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php教程图片文件上传实例代码</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <label for="filefield"></label>
  <input type="file" name="file" id="filefield" />
  <input type="submit" name="button" id="button" value="文件上传" />
</form>
</body>
</html>


<?

 代码如下 复制代码
if( $_post )
{
 if( $_files['file'] )
 {
  $filename = $_files['file']['type'];
  $newname ='111cn.net.gif';
  if( strtolower( $filename) == 'image/gif' )
  {
   if( move_uploaded_file($_files['file']['tmp_name'] ,$newname) )
   {
    //$ext = getimagesize( $newname );
    //print_r($ext);
    echo '图片上传成功';
   }
   else
   {
    die('图片上传失败!');
   }
  }
  else
  {
   exit('本程序只允许上传gif图片');
  }
 }
}


/*
move_uploaded_file 语法参考地址
http://www.111cn.net/phper/24/da78cda75379943ff126f6af13cf5aa9.htm
strtolower实例地址
http://www.111cn.net/phper/21/805cd7d41d20a10b71b35e1a8f2b8596.htm
$_files 说有
$_files[''myfile''][''name'']   客户端文件的原名称。
$_files[''myfile''][''type'']   文件的 mime 类型,需要浏览器提供该信息的支持,例如"image/gif"。
$_files[''myfile''][''size'']   已上传文件的大小,单位为字节。
$_files[''myfile''][''tmp_name'']   文件被上传后在服务端储存的临时文件名,一般是系统默认。可以在php.ini的upload_tmp_dir 指定,但 用 putenv() 函数设置是不起作用的。
$_files[''myfile''][''error'']   和该文件上传相关的错误代码。[''error''] 是在 php 4.2.0 版本中增加的

*/
?>

本站原创教程转载注明来源的于http://www.111cn.net/phper/php.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<head>

 代码如下 复制代码
<script type="text/javascript教程">
function showUser(str){
    if(window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{ // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","ajax.php",true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
    xmlhttp.send('q='+str);
}
</script>

</head>

 代码如下 复制代码

<body>
<form method="post">
ajax的POST<input name="submit" type="submit" onclick="showUser(28);return false;" value="查询"/>
</form>
<form action="ajax.php" method="post">
真正的POST<input name="q" type="submit" value="28"/>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
php 代码

<?php
header('Content-Type:text/html;charset=utf-8;');
echo "POST数据: ".implode('',file('php://input'));
echo  "<br>POST[q] ".$_POST['q']."<br>";
print_r($_POST);
?>

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • C#使用StreamWriter写入文件的方法

    这篇文章主要介绍了C#使用StreamWriter写入文件的方法,涉及C#中StreamWriter类操作文件的相关技巧,需要的朋友可以参考下...2020-06-25
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
  • C#路径,文件,目录及IO常见操作汇总

    这篇文章主要介绍了C#路径,文件,目录及IO常见操作,较为详细的分析并汇总了C#关于路径,文件,目录及IO常见操作,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01