当前位置:首页 > 网络编程 > WEB编程 > PHP > PHP脚本数据库功能详解(中)

PHP脚本数据库功能详解(中)

点击次数:41 次 发布日期:2008-11-13 09:33:40 作者:源代码网
源代码网推荐

源代码网整理以下利用PHP将文件保存到数据库
数据库是数据组织、存储的中心。将要处理的也可能是各种数据,包括程序、文件、报表,甚至音频、视频数据。由于通过浏览器,个人用户只能填写少部分的个人简历。因此,我们这里示范用户个人简历上载的功能。其他类型的数据可以模仿此例进行操作。

源代码网整理以下首先是信息收集页面。让用户选择要上载的文件。此页面的html代码如下:

源代码网整理以下

以下为引用的内容:

源代码网整理以下  〈!-- begin of post.htm--〉

源代码网整理以下  〈p〉 〈/p〉

源代码网整理以下  〈form method="POST" action="insert.php" ENCTYPE="multipart/form-data"〉

源代码网整理以下  〈p〉〈b〉个人简历提交〈/b〉〈/p〉

源代码网整理以下  〈p〉姓名:〈br〉

源代码网整理以下  〈input type="text" name="Name" size="20"〉〈/p〉

源代码网整理以下  〈p〉个人简介:〈br〉

源代码网整理以下  〈textarea rows="2" name="Intro" cols="20"〉〈/textarea〉〈/p〉

源代码网整理以下  〈p〉简历文件:〈br〉

源代码网整理以下  〈input type="file" name="ResuFile"〉〈/p〉

源代码网整理以下  〈p〉〈input type="submit" value="提交" name="B1"〉〈/p〉

源代码网整理以下  〈/form〉

源代码网整理以下  〈!-End of post.htm--〉

源代码网整理以下注意,ENCTYPE关键字一定不能省,否则文件无法正确上载。

源代码网整理以下这里,我们再把向数据库插入记录的代码重新设计:

源代码网整理以下

以下为引用的内容:

源代码网整理以下  〈?

源代码网整理以下  //begin of file insert.php

源代码网整理以下  if($ResuFile != "none")

源代码网整理以下  //确定用户选择了文件

源代码网整理以下  {

源代码网整理以下  $Size = filesize($ResuFile);

源代码网整理以下  //确定文件大小

源代码网整理以下  $mFileData = addslashes(fread(fopen($ResuFile, "r"), $Size));

源代码网整理以下  //读取文件,对内容进行处理

源代码网整理以下  unlink($ResuFile);

源代码网整理以下  //删除上载临时文件

源代码网整理以下  }

源代码网整理以下     $LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");

源代码网整理以下  $DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");

源代码网整理以下  $query = "insert into Resume(Name,Intro,ResuFile) values("$Name", "$Intro", "$mFileData")";

源代码网整理以下  $result = @mysql_query("$query",$LinkID); //执行查询,插入文件到数据库

源代码网整理以下  if(! $result)

源代码网整理以下   echo "数据插入失败!";

源代码网整理以下  else

源代码网整理以下   echo "文件上载成功!";

源代码网整理以下  @mysql_close($LinkID);

源代码网整理以下  //end of file insert.php

源代码网整理以下  ?〉

源代码网整理以下有了上面的基础,写出从数据库读数据的程序应该很简单了。需要注意的是文件向客户发送的方法。服务器必须向浏览器发送头信息,说明将要发送的数据为word文档。如果用户计算机装有MSWord,浏览器将自动调用word进行文档显示。

源代码网整理以下我们可以设置一个超级链接,来下载这个Word文件:

源代码网整理以下

以下为引用的内容:

源代码网整理以下  〈?

源代码网整理以下  //begin of file show.php

源代码网整理以下     $LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");

源代码网整理以下  $DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");

源代码网整理以下  $query = "insert into Resume(Name,Intro,ResuFile) values("$Name", "$Intro", "$mFileData")";

源代码网整理以下  $result = @mysql_query("$query",$LinkID);

源代码网整理以下  //执行查询,插入文件到数据库

源代码网整理以下  $query= "select ID,Name,Intro from Resume";

源代码网整理以下  //生成SQL语句

源代码网整理以下  $result = mysql_query($query,$LinkID); //执行,结果集保存到变量$result中

源代码网整理以下  $num= mysql_num_rows($result); //取得查询返回的记录行数

源代码网整理以下  if($num == 0)

源代码网整理以下  {

源代码网整理以下   echo "没有找到任何记录";

源代码网整理以下   exit();

源代码网整理以下  }

源代码网整理以下  while($row=mysql_fetch_array($result)) //取结果集的下一行数据到数组$row中

源代码网整理以下  {

源代码网整理以下   echo $row["ID"]." ".$row["Name"]." ".$row["Intro"]." ";

源代码网整理以下   echo "〈a href= "download.php?ID=".$row["ID"].""〉查看Word文档〈/a〉〈br〉";

源代码网整理以下  }

源代码网整理以下  //end of file show.php

源代码网整理以下  ?〉

源代码网整理以下 源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华