Simple file upload to MySql database developed in PHP (4)
In the previous section, we set up several custom functions and implemented the generation of new file addresses.
Here we need to reference the custom functions to complete the entire file upload function
Need to use the include_once statement
The include_once statement includes and runs the specified file during script execution. This behavior is similar to the include statement, the only difference is that if the file has already been included, it will not be included again. As the name of this statement implies, it will only be included once.
include_once can be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.
We created the upload.php file in the previous section
Here we need to reference this file
<?php include_once('upload.php'); ?>
The following is the complete display code:
<?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 == "") //判断是否在标题中添加内容 echo"<Script>window.alert('对不起!你输入的信息不完整!');history.back()</Script>"; $sql = "insert into img(title,pic) values('$title','$pic')"; //向数据库中添加文件内容 $result = mysqli_query($link,$sql); } ?> <html> <head> <meta charset="utf-8"> <title>文件上传实例</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>标题:</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>