Heim  >  Artikel  >  Backend-Entwicklung  >  PHP脚本数据库功能详解2_PHP教程

PHP脚本数据库功能详解2_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:23:09872Durchsuche

(作者:王凯波)   利用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   ?〉   访问文件show.php,用户看到的是个人简要信息的列表。点击“查看Word文档”,即可看到对应成员详细的个人简历。   Word文档的显示是用下面的文件:   〈?   // begin of file download.php   $LinkID=@mysql_connect("localhost", "root" , "") or die("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");   $DBID = @mysql_select_db("ResumeDB",$LinkID) or die("选择数据库出错,可能是您指定的数据库不存在!");   $query = "select ResuFile from Resume where ID=$ID";   //$ID为调用传递的变量   $result = @mysql_query("$query",$LinkID);   //执行查询,从数据库读取文件内容   if(mysql_num_rows($result) 〈 1 )   {    echo "没有找到相应的文件!";    exit();   }   $row = mysql_fetch_array($result);   $mFileData = $row["ResuFile"];   //读取个人简历的内容(Word文件格式的数据)   header("Content-type: application/msword");   //发送头信息,说明将要发送的数据为word文档   echo $mFileData;   //发送文档数据   //end of file download.php   ?〉   至此,我们已经实现了个人简历的提交、数据库存储、信息浏览等功能,基本完成了“人才信息交流”的框架功能。   需要说明的是,通过PHP进行文件上载及数据库存储是个较突出的技术难题。很多关于PHP的网站都不断出现这类问题。这些操作,对平台、环境设置依赖性较大。不同的平台配置,都可能导致操作的失败。本文后面附了上述程序的运行平台、编译参数,以供参考。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/532260.htmlTechArticle(作者:王凯波) 利用PHP将文件保存到数据库 数据库是数据组织、存储的中心。将要处理的也可能是各种数据,包括程序、文件、报表,...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn