PHP开发之简单文件上传到MySql数据库(四)
前一节我们设置了几个自定义函数,并且实现了实现生成新文件地址,
这里我们就需要引用自定义函数,来完成整个的文件上传功能
需要使用include_once 语句
include_once 语句在脚本执行期间包含并运行指定文件。此行为和include语句类似,唯一区别是如果该文件中已经被包含过,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
include_once可以用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。
上一节我们创建了upload.php文件
这里我们需要引用这个文件
<?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 == "") //判断是否在标题中添加内容
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>
neue Datei
<?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>
Vorschau
Clear
- Kursempfehlungen
- Kursunterlagen herunterladen
Die Kursunterlagen stehen derzeit nicht zum Download zur Verfügung. Die Mitarbeiter organisieren es derzeit. Bitte schenken Sie diesem Kurs in Zukunft mehr Aufmerksamkeit
Auch Studierende, die diesen Kurs gesehen haben, lernen
Lassen Sie uns kurz über die Gründung eines Unternehmens in PHP sprechen
Kurze Einführung in die Web-Frontend-Entwicklung
Umfangreiche, praktische Tianlongbabu-Entwicklung eines Mini-Version-MVC-Frameworks, das die Enzyklopädie-Website mit peinlichen Dingen imitiert
Erste Schritte mit der praktischen PHP-Entwicklung: Schnelle PHP-Erstellung [Small Business Forum]
Anmeldebestätigung und klassisches Message Board
Wissenssammlung über Computernetzwerke
Schnellstart-Node.JS-Vollversion
Der Frontend-Kurs, der Sie am besten versteht: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Schreiben Sie Ihr eigenes PHP-MVC-Framework (40 Kapitel ausführlich/große Details/Muss gelesen werden, damit Neulinge vorankommen)
















