上一篇:PHP脚本与数据库功能详解(上)
利用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("不能连接到数据库服务器!可能是数据库服务器没有启动,或者用户名密码有误!");
波比源码 » PHP脚本与数据库功能详解(中)
generic levofloxacin buy levofloxacin sale
spironolactone tablet finasteride pill diflucan 200mg sale
order generic fildena 100mg order generic fildena 50mg buy robaxin without prescription
buy cialis 40mg online cheap buy cheap cialis viagra sildenafil 25mg
buy anastrozole 1mg online order anastrozole 1 mg sale female viagra cvs
order prograf 5mg prograf uk buy ursodiol 300mg pill
order accutane 20mg online cheap order prednisone 10mg for sale prednisone 5mg drug
order piracetam 800 mg sale nootropil 800 mg over the counter cheap viagra without prescription
order cialis 20mg pills buy clomipramine 25mg generic anafranil generic
itraconazole pills prometrium online buy generic tinidazole
buy glycomet 1000mg without prescription cheap cialis 40mg order cialis 5mg without prescription
buy omeprazole 20mg online cheap play money poker online real casino online
xenical 120mg pill cheap zovirax 800mg buy acyclovir for sale
tadalafil 20mg without prescription cialis 10mg ca clopidogrel 75mg brand
purchase lioresal sale order toradol 10mg online cheap purchase toradol sale
buy gloperba pills colchicine pills blackjack online game
tadalafil pill buy cipro 1000mg online cheap cipro 1000mg price
buy generic cephalexin 500mg buy keflex 250mg generic buy erythromycin
golden nugget online casino slots games order tadalafil 5mg sale