PHP實現(xiàn)上傳文件并存進數(shù)據(jù)庫的方法
更新時間:2015年07月16日 09:54:48 作者:宋勇野
這篇文章主要介紹了PHP實現(xiàn)上傳文件并存進數(shù)據(jù)庫的方法,涉及php文件上傳所涉及的文件與表單操作及數(shù)據(jù)庫操作相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了PHP實現(xiàn)上傳文件并存進數(shù)據(jù)庫的方法。分享給大家供大家參考。具體如下:
show_add.php文件如下:
<?php if(!isset($_REQUEST['id']) or $_REQUEST['id']=="") die("error: id none"); $id = $_REQUEST['id']; //定位記錄,讀出 $conn=mysql_connect("localhost","root","admin"); if(!$conn) die("error: mysql connect failed"); mysql_select_db("nokiapaymentplat",$conn); $sql = "select * from receive where id=$id"; $result = mysql_query($sql,$conn); if(!$result) die("error: mysql query"); $num=mysql_num_rows($result); if($num<1) die("error: no this recorder"); $data = mysql_result($result,0,"file_data"); $type = mysql_result($result,0,"file_type"); $name = mysql_result($result,0,"file_name"); mysql_close($conn); //先輸出相應(yīng)的文件頭,并且恢復(fù)原來的文件名 header("Content-type:$type"); header("Content-Disposition: attachment; filename=$name"); echo $data; ?>
show_info.php文件如下:
<?php if(!isset($_REQUEST['id']) or $_REQUEST['id']=="") die("error: id none"); $id = $_REQUEST['id']; //定位記錄,讀出 $conn=mysql_connect("localhost","root","admin"); if(!$conn) die("error: mysql connect failed"); mysql_select_db("nokiapaymentplat",$conn); $sql = "select file_name ,file_size from receive where id=$id"; $result = mysql_query($sql,$conn); if(!$result) die(" error: mysql query"); //如果沒有指定的記錄,則報錯 $num=mysql_num_rows($result); if($num<1) die("error: no this recorder"); //下面兩句程序也可以這么寫 //$row=mysql_fetch_object($result); //$name=$row->name; //$size=$row->size; $name = mysql_result($result,0,"file_name"); $size = mysql_result($result,0,"file_size"); mysql_close($conn); echo "<hr>上傳的文件的信息:"; echo "<br>The file's name - $name"; echo "<br>The file's size - $size"; echo "<br><a href=show_add.php?id=$id>附件</a>"; ?>
submit.php文件如下:
<?php if(is_uploaded_file($_FILES['myfile']['tmp_name'])) { //有了上傳文件了 $myfile=$_FILES["myfile"]; //設(shè)置超時限制時間,缺省時間為 30秒,設(shè)置為0時為不限時 $time_limit=60; set_time_limit($time_limit); // //把文件內(nèi)容讀到字符串中 $fp=fopen($myfile['tmp_name'], "rb"); if(!$fp) die("file open error"); $file_data = addslashes(fread($fp, filesize($myfile['tmp_name']))); fclose($fp); unlink($myfile['tmp_name']); //文件格式,名字,大小 $file_type=$myfile["type"]; $file_name=$myfile["name"]; $file_size=$myfile["size"]; die($file_type); //連接數(shù)據(jù)庫,把文件存到數(shù)據(jù)庫中 $conn=mysql_connect("localhost","root","admin"); if(!$conn) die("error : mysql connect failed"); mysql_select_db("nokiapaymentplat",$conn); $sql="insert into receive (file_data,file_type,file_name,file_size) values ('$file_data','$file_type','$file_name',$file_size)"; $result=mysql_query($sql,$conn); //下面這句取出了剛才的insert語句的id $id=mysql_insert_id(); mysql_close($conn); set_time_limit(30); //恢復(fù)缺省超時設(shè)置 echo "上傳成功--- "; echo "<a href='show_info.php?id=$id'>顯示上傳文件信息</a>"; } else { echo "你沒有上傳任何文件"; } ?>
upload.php文件如下:
<html> <head> <title>文件上傳表單</title> </head> <body> <table> <form enctype='multipart/form-data' name='myform' action='submit.php' method='post'> <INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000"> <tr><td>選擇上傳文件</td><td> <input name='myfile' type='file'></td></tr> <tr><td colspan='2'><input name='submit' value='上傳' type='submit'></td></tr> </table> </body> </html>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- php將csv文件導(dǎo)入到mysql數(shù)據(jù)庫的方法
- php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法
- php簡單操作mysql數(shù)據(jù)庫的類
- PHP基于MySQL數(shù)據(jù)庫實現(xiàn)對象持久層的方法
- php實現(xiàn)mysql數(shù)據(jù)庫分表分段備份
- PHP將Excel導(dǎo)入數(shù)據(jù)庫及數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出至Excel的方法
- PHP使用ODBC連接數(shù)據(jù)庫的方法
- php實現(xiàn)將Session寫入數(shù)據(jù)庫
- 1億條數(shù)據(jù)如何分表100張到Mysql數(shù)據(jù)庫中(PHP)
- 初識通用數(shù)據(jù)庫操作類——前端easyui-datagrid,form(php)
- php基于session實現(xiàn)數(shù)據(jù)庫交互的類實例
- php頁面,mysql數(shù)據(jù)庫轉(zhuǎn)utf-8亂碼,utf-8編碼問題總結(jié)
- 常見PHP數(shù)據(jù)庫解決方案分析介紹
相關(guān)文章
PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼
本篇文章是對PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP curl_setopt()函數(shù)實例代碼與參數(shù)分析
curl_setopt()函數(shù)將為一個CURL會話設(shè)置選項。option參數(shù)是你想要的設(shè)置,value是這個選項給定的值。2011-06-06