PHP開發(fā)之簡單檔案上傳到MySql資料庫(四)
前一節(jié)我們?cè)O(shè)定了幾個(gè)自訂函數(shù),並且實(shí)作了實(shí)作產(chǎn)生新檔案位址,
這裡我們就需要引用自訂函數(shù),來完成整個(gè)的檔案上傳功能
#需要使用include_once 語句
include_once 語句在腳本執(zhí)行期間包含並執(zhí)行指定檔案。此行為和include語句類似,唯一差異是如果該檔案中已經(jīng)被包含過,則不會(huì)再次包含。如同此語句名字暗示的那樣,只會(huì)包含一次。?
include_once可以用於在腳本執(zhí)行期間同一個(gè)檔案有可能被包含超過一次的情況下,想確保它只被包含一次以避免函數(shù)重定義,變數(shù)重新賦值等問題。
上一節(jié)我們建立了upload.php檔案
這裡我們需要引用這個(gè)檔案
<?php include_once('upload.php'); ?>
下面是完整的展示程式碼:
<?php header("content-type:text/html;charset=utf8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("連接失敗:".mysqli_connect_error()); } $action = isset($_GET['action'])?$_GET['action']:""; if ($action == "save"){ include_once('uploadclass.php'); //引入外部文件 $title = $_POST['title']; $pic = $uploadfile; if($title == "") //判斷是否在標(biāo)題中添加內(nèi)容 echo"<Script>window.alert('對(duì)不起!你輸入的信息不完整!');history.back()</Script>"; $sql = "insert into img(title,pic) values('$title','$pic')"; //向數(shù)據(jù)庫中添加文件內(nèi)容 $result = mysqli_query($link,$sql); } ?> <html> <head> <meta charset="utf-8"> <title>文件上傳實(shí)例</title> <style type="text/css"> <!-- body { font-size: 20px; } input { background-color: #66CCFF; border: 1px inset #CCCCCC; } form { margin-top:5%; } --> </style> </head> <body> <form method="post" action="?action=save" enctype="multipart/form-data"> <table border=0 cellspacing=0 cellpadding=0 align=center width="100%"> <tr> <td width=55 height=20 align="center"></td> <td height="16"> <table> <tr> <td>標(biāo)題:</td> <td><input name="title" type="text" id="title"></td> </tr> <tr> <td>文件: </td> <td><label> <input name="file" type="file" value="瀏覽" > <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> </label></td> </tr> <tr> <td></td> <td><input type="submit" value="上 傳" name="upload"></td> </tr> </table> </td> </tr> </table> </form> </body> </html>