(作者:王凯波)
利用PHP将文件保存到数据库
数据库是数据组织、存储的中心。将要处理的也可能是各种数据,包括程序、文件、报表,甚至音频、视频数据。由于通过浏览器,个人用户只能填写少部分的个人简历。因此,我们这里示范用户个人简历上载的功能。其他类型的数据可以模仿此例进行操作。
首先是信息收集页面。让用户选择要上载的文件。此页面的html代码如下:
〈!-- begin of post.htm--〉
〈p〉 〈/p〉
〈form method=%26quot;POST%26quot; action=%26quot;insert.php%26quot; ENCTYPE=%26quot;multipart/form-data%26quot;〉
〈p〉〈b〉个人简历提交〈/b〉〈/p〉
〈p〉姓名:〈br〉
〈input type=%26quot;text%26quot; name=%26quot;Name%26quot; size=%26quot;20%26quot;〉〈/p〉
〈p〉个人简介:〈br〉
〈textarea rows=%26quot;2%26quot; name=%26quot;Intro%26quot; cols=%26quot;20%26quot;〉〈/textarea〉〈/p〉
〈p〉简历文件:〈br〉
〈input type=%26quot;file%26quot; name=%26quot;ResuFile%26quot;〉〈/p〉
〈p〉〈input type=%26quot;submit%26quot; value=%26quot;提交%26quot; name=%26quot;B1%26quot;〉〈/p〉
〈/form〉
〈!-End of post.htm--〉
注意,ENCTYPE关键字一定不能省,否则文件无法正确上载。
这里,我们再把向数据库插入记录的代码重新设计:
〈?
//begin of file insert.php
if($ResuFile != %26quot;none%26quot;)
//确定用户选择了文件
{
$Size = filesize($ResuFile);
//确定文件大小
$mFileData = addslashes(fread(fopen($ResuFile, %26quot;r%26quot;), $Size));
//读取文件,对内容进行处理
unlink($ResuFile);
//删除上载临时文件
}
$LinkID=@mysql_connect(%26quot;localhost%26quot;, %26quot;root%26quot; , %26quot;%26quot;) or die(%26quot;不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!%26quot;);
$DBID = @mysql_select_db(%26quot;ResumeDB%26quot;,$LinkID) or die(%26quot;选择数据库出错,可能是您指定的数据库不存在!%26quot;);
$query = %26quot;insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')%26quot;;
$result = @mysql_query(%26quot;$query%26quot;,$LinkID); //执行查询,插入文件到数据库
if(! $result)
echo %26quot;数据插入失败!%26quot;;
else
echo %26quot;文件上载成功!%26quot;;
@mysql_close($LinkID);
//end of file insert.php
?〉
有了上面的基础,写出从数据库读数据的程序应该很简单了。需要注意的是文件向客户发送的方法。服务器必须向浏览器发送头信息,说明将要发送的数据为word文档。如果用户计算机装有MSWord,浏览器将自动调用word进行文档显示。
我们可以设置一个超级链接,来下载这个Word文件:
〈?
//begin of file show.php
$LinkID=@mysql_connect(%26quot;localhost%26quot;, %26quot;root%26quot; , %26quot;%26quot;) or die(%26quot;不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!%26quot;);
$DBID = @mysql_select_db(%26quot;ResumeDB%26quot;,$LinkID) or die(%26quot;选择数据库出错,可能是您指定的数据库不存在!%26quot;);
$query = %26quot;insert into Resume(Name,Intro,ResuFile) values('$Name', '$Intro', '$mFileData')%26quot;;
$result = @mysql_query(%26quot;$query%26quot;,$LinkID);
//执行查询,插入文件到数据库
$query= %26quot;select ID,Name,Intro from Resume%26quot;;
//生成SQL语句
$result = mysql_query($query,$LinkID); //执行,结果集保存到变量$result中
$num= mysql_num_rows($result); //取得查询返回的记录行数
if($num == 0)
{
echo %26quot;没有找到任何记录%26quot;;
exit();
}
while($row=mysql_fetch_array($result)) //取结果集的下一行数据到数组$row中
{
echo $row[%26quot;ID%26quot;].%26quot; %26quot;.$row[%26quot;Name%26quot;].%26quot; %26quot;.$row[%26quot;Intro%26quot;].%26quot; %26quot;;
echo %26quot;〈a href= %26quot;download.php?ID=%26quot;.$row[%26quot;ID%26quot;].%26quot;%26quot;〉查看Word文档〈/a〉〈br〉%26quot;;
}
//end of file show.php
?〉
访问文件show.php,用户看到的是个人简要信息的列表。点击“查看Word文档”,即可看到对应成员详细的个人简历。
Word文档的显示是用下面的文件:
〈?
// begin of file download.php
$LinkID=@mysql_connect(%26quot;localhost%26quot;, %26quot;root%26quot; , %26quot;%26quot;) or die(%26quot;不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!%26quot;);
$DBID = @mysql_select_db(%26quot;ResumeDB%26quot;,$LinkID) or die(%26quot;选择数据库出错,可能是您指定的数据库不存在!%26quot;);
$query = %26quot;select ResuFile from Resume where ID=$ID%26quot;;
//$ID为调用传递的变量
$result = @mysql_query(%26quot;$query%26quot;,$LinkID);
//执行查询,从数据库读取文件内容
if(mysql_num_rows($result) 〈 1 )
{
echo %26quot;没有找到相应的文件!%26quot;;
exit();
}
$row = mysql_fetch_array($result);
$mFileData = $row[%26quot;ResuFile%26quot;];
//读取个人简历的内容(Word文件格式的数据)
header(%26quot;Content-type: application/msword%26quot;);
//发送头信息,说明将要发送的数据为word文档
echo $mFileData;
//发送文档数据
//end of file download.php
?〉
至此,我们已经实现了个人简历的提交、数据库存储、信息浏览等功能,基本完成了“人才信息交流”的框架功能。
需要说明的是,通过PHP进行文件上载及数据库存储是个较突出的技术难题。很多关于PHP的网站都不断出现这类问题。这些操作,对平台、环境设置依赖性较大。不同的平台配置,都可能导致操作的失败。本文后面附了上述程序的运行平台、编译参数,以供参考。
PHP脚本数据库功能详解(2)
来源:[标签:出处]
作者:[标签:作者]
时间:2008-02-19
点击:
0
最新评论共有 0 位网友发表了评论
查看所有评论
发表评论
- 栏目列表
-
热点关注

