DROP TABLE IF EXISTS movies;
CREATE TABLE movies (
movieId INTEGER NOT NULL AUTO_INCREMENT,
title VARCHAR( 255 ),
source VARCHAR( 255 ),
thumb VARCHAR( 255 ),
width INTEGER,
height INTEGER,
PRIMARY KEY( movieId )
);
mysql movies < movies.sql
<html>
<body>
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<table>
<tr><td>Title</td><td><input type="text" name="title"></td></tr>
<tr><td>Movie</td><td><input type="file" name="movie"></td></tr>
</table>
<input type="submit" value="Upload" />
</form>
</body>
</html>
<html><body>
<?php
require "DB.php";
function converttoflv( $in, $out )
{
unlink( $out );
$cmd = "ffmpeg -v 0 -i $in -ar 11025 $out 2>&1";
$fh = popen( $cmd, "r" );
while( fgets( $fh ) ) { }
pclose( $fh );
}
function getthumbnail( $in, $out )
{
unlink( $out );
$cmd = "ffmpeg -i $in -pix_fmt rgb24 -vframes 1 -s 300x200 $out 2>&1";
$fh = popen( $cmd, "r" );
while( fgets( $fh ) ) { }
pclose( $fh );
}
function flv_import( $upfile, $fname, $title )
{
$fname = preg_replace( '/..*$/', '', basename( $fname ) );
$flvpath = "$fname.flv";
$thumbpath = "$fname.gif";
converttoflv( $upfile, "movies\$flvpath" );
getthumbnail( $upfile, "movies\$thumbpath" );
$dsn = 'mysql://root@localhost/movies';
$db =& DB::connect( $dsn );
if ( PEAR::isError( $db ) ) { die($db->getMessage()); }
$sth = $db->prepare( 'INSERT INTO movies VALUES ( 0, ?, ?, ?, ?, ? )' );
$db->execute( $sth, array( $title, $flvpath, $thumbpath, 300, 200 ) );
}
flv_import( $_FILES['movie']['tmp_name'], $_FILES['movie']['name'], $_POST['title'] );
?>
File sucessfully uploaded
</body></html>
Listing 4. simplemovie.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:VBox backgroundColor="white" width="400" height="335"> <mx:VideoDisplay width="400" height="300" id="videoPlayer" source="{Application.application.parameters.movie}" /> <mx:HBox width="100%" horizontalAlign="center"> <mx:Button label="Play" click="videoPlayer.play()" /> </mx:HBox> </mx:VBox> </mx:Application>
Listing 5. simptest.php
<?php require "DB.php"; $moviebase = 'http://localhost:8080/movies/'; $dsn = 'mysql://root@localhost/movies'; $db =& DB::connect( $dsn ); if ( PEAR::isError( $db ) ) { die($db->getMessage()); } $source = null; $movieId = 1; if ( array_key_exists( 'movie', $_GET ) ) $movieId = $_GET['movie']; $movies = array(); $res = $db->query( 'SELECT movieId, source, title FROM movies' ); while( $row = $res->fetchrow( ) ) { $movies []= $row; if ( $row[0] == $movieId ) $source = $row[1]; } if ( $source == null ) $source = $movies[0][1]; ?> <html> <body> <table> <tr><td valign="top"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="335" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> <param name="movie" value="simplemovie.swf" /> <param name="quality" value="high" /> <param name="flashVars" value="movie=<?php echo( $moviebase.$source ) ?>"> <embed" width=100% src="simplemovie.swf" quality="high" width="400" height="335" play="true" loop="false" quality="high" flashVars="movie=<?php echo( $moviebase.$source ) ?>" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> </embed> </object> </td><td valign="top"> <?php foreach( $movies as $movie ) { ?> <a href="simptest.php?movie=<?php echo( $movie[0] )?>"><?php echo( $movie[2] )?></a><br/> <?php } ?> </td></tr></table> </body> </html>
一直用smarty的cache,但感觉还是要自己做一个,才有感觉。网上有很多牛人的功能比较完备,打算先自己搞简单的再慢慢丰满。这两天做了一个比较简单的,在hi.baidu.net/alex_wang58记录一下。
一、用到的相关技术关键词:PHP, Apache,
mod_rewrite (RewriteCond,RewriteRule)地址重写,
ob系列函数缓冲
file_put_contents生成html
二、流程:用户发出请求url?id=x ,判断文章是否存在
(1)存在则直接转到对应的Html页面。
(2)不存在通过php读取数据库数据,然后生成html文件,并存放到指定目录。
三、实现方法:
(1)地址重写用Apahce的mod_rewrite模块中的RewriteRule指令实现重写(mod_rewrite的开启和简单规则见本博另一篇http://hi.baidu.com/alex%5Fwang5 ... 0346ffb3fb952e.html )。
(2)判断文章是否存在用Apahce 的mod_rewrite模块中的RewriteCond指令
(3)生成html文件:
ob_star()打开缓冲,将读取文章的php包含进来,然后用file_put_contents将获得的缓冲内容写入指定HTMl文件。
四、代码
/Test 目录下的 .htaccess 文件内容:
RewriteEngine On
RewriteRule ^index.html$ /news.php [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^html/news_([0-9]+).html$ getnews.php?id=$1 [L]
对news.php的访问将通过 localhost/Test/index.html 实现由第二句 RewriteRule ^index.html$ Test/news.php [L] 实现
news.php =============================> news.php将列出文章标题链接。
复制PHP内容到剪贴板
PHP代码:
<?php
header("Content-Type:text/html; charset=gbk"); //以防出现乱码
mysql_connect("localhost","root","");
mysql_query('SET NAMES gbk'); //我的数据库用的gbk编码,请根据自己实际情况调整
mysql_select_db("test");
$sql = "SELECT `id`,`title` FROM `arc` order by `id` DESC";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs) ){
echo "<a href='/Test/html/news_$row[id].html'>$row[title]</a><br>";
}
?>
比如生成了<a href='/Test/html/news_3.html'>php静态页实现</a>
当点击链接发出对 http://localhost/Test/html/news_3.html 的请求时
Apache将会判断 news_3.html 是否存在,由 .htaccess中的第三句
RewriteCond %{REQUEST_FILENAME} !-s
实现:
RewriteCond 是“定向重写发生条件”。REQUEST_FILENAME 这个参数是“客户端请求的文件名”
'-s' (是一个非空的常规文件[size]) 测试指定文件是否存在而且是一个尺寸大于0的常规的文件. !表示匹配条件的反转。
所以 RewriteCond 这句就表示当请求链接不存在时 执行下面的 RewriteRule 规则。
所以当请求的news_3.html 不存在时会重写地址让 getnews.php?id=3 来处理(否则如果news_3.html 存在则直接就加载该html文件)。
getnews.php ===================>功能:判断参数传输的完整性,并调用相应文件生成html文件。
复制PHP内容到剪贴板
PHP代码:
<?php
$id =$_GET['id'];
$root =& $_SERVER['DOCUMENT_ROOT'];
$filename = "news_".$id.".html";
$file = $root."/Test/html/".$filename;
ob_start();
include($root."/Test/newsDetail.php");
file_put_contents($file,ob_get_contents());
ob_end_flush();
?>
newsDetail.php ====================> 从数据库中读取数据,产生新闻内容,内容被getnews.php捕获
复制PHP内容到剪贴板
PHP代码:
<?php
header("Content-Type:text/html; charset=gbk");
if( isset($_GET['id']) ){
$id = & $_GET['id'];
}else{
header("Location: [url]http://127.0.0.1/lean/Test/html/news_failed.html[/url]");
exit();
}
mysql_connect("localhost","root","");
mysql_query('SET NAMES gbk');
mysql_select_db("test");
$id =$_GET['id'];
$sql = "SELECT `news` FROM `arc` WHERE `id`=$id";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs) ){
echo $row['news'];
}
?>
这样将会在/Test/html 目录下产生以 news_文章ID.html 命名的html文件。
PS: 一开始在判断是否存在相应html页面时采用的是 php 内置的 file_exists() 判断,而不用Apache的 RewriteCond,也即没有 RewriteCond %{REQUEST_FILENAME} !-s。看似可行,但结果会产生“循环重定向”的问题。
当news_3.html 不存在时 我们需要用 getnews.php生成news_3.html ,生成完毕后需要转向到 news_3.html ,于是又形成了一次请求mod_rewrite又启动把 news_3.html重写为 getnews.php?id=3 这就形成了死循环了。所以把文件存在性的判断交给 RewriteCond ,指定的html文件不存在时才启用重写规则。这样循环重定向的问题就没有了。
一开始没有采用fopen打开newsDetail.php,然后再将生成的内容fwrite成html文件,然后include输出静态页面。后来在fhjr999的提醒下,改为:将newDetail.php包含进getnews.php,通过ob系列函数将生成的内容放入缓冲,然后再生成html文件。ob的效率是前者的20倍左右。
Web开发是今后分布式程式开发的主流,通常的web开发都要涉及到与数据库打交道,客户端从服务器端读取通常都是以分页的形式来显示,一页一页的阅读起来既方便又美观。所以说写分页程序是web开发的一个重要组成部分,在这里,我们共同来研究分页程序的编写。
一、分页程序的原理
分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page)。有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在mysql里假如要想取出表内某段特定内容可以使用的 T-SQL语句:select * from table limit offset,rows来实现。这里的offset是记录偏移量,它的计算方法是offset=$pagesize*($page-1),rows是要显示的记录条数,这里就是$page。也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录开始的20条记录。
二、主要代码解析
$pagesize=10; //设置每一页显示的记录数
$conn=mysql_connect("localhost","root",""); //连接数据库
$rs=mysql_query("select count(*) from tb_product",$conn); //取得记录总数$rs
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
//判定页数设置
if (isset($_GET['page'])){
$page=intval($_GET['page']);
}
else{
$page=1; //否则,设置为第一页
}
三、创建用例用表myTable
create table myTable(id int NOT NULL auto_increment,news_title varchar(50),news_cont text,add_time datetime,PRIMARY KEY(id))
四、完整代码
<html>
<head>
<title>php分页示例</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<?php
$conn=mysql_connect("localhost","root","");
//设定每一页显示的记录数
$pagesize=1;
mysql_select_db("mydata",$conn);
//取得记录总数$rs,计算总页数用
$rs=mysql_query("select count(*) from tb_product",$conn);
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages ;
//设置页数
if (isset($_GET['page'])){
$page=intval($_GET['page']);
}
else{
//设置为第一页
$page=1;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>显示文件夹内文件</title>
</head>
<body>
<?php
$sort_file=array();
$filefj=array();
define('FILEPATH','../filehandle/uploadfile/');
$sortlist=trim($_GET["sort"]);
$dir=opendir(FILEPATH);
echo'文件上传位置:'.FILEPATH.'<br/>';
echo'文件列表:'.'<ul>';
if($sortlist == 'filetype'){
while (($file=readdir($dir))!== false) {
if ($file != "." && $file != "..") {
$file=basename($file);
$sort_file[]=$file;
}
}
foreach ($sort_file as $key=>$var) {
$filefj[]=explode('.',$var);
}
usort($filefj,'compare');
foreach ($filefj as $key=>$val){
$combinationfile=implode('.',$val);
echo '<li>'. $combinationfile.' </li>';
}
}else{
while (($file=readdir($dir))!== false) {
if ($file != "." && $file != "..") {
$file=basename($file);
echo '<li>'.$file.$file_size.' </li>';
}
}
}
echo '</ul>';
closedir($dir);
echo'</br>';
echo'<a href="index.php?sort=filetype">按文件类型排序</a> <a href="index.php">按正常文件排序</a> <a href="upfile.html">继续上传文件</a>';
function compare($x,$y){
if($x[1]==$y[1])
return 0;
else if ($x[1]<$y[1])
return -1;
else return 1;
}
?>
</body>
</html>
文件效果就不说了,自己试一一就知道了.