PHP로 개발된 MySql 데이터베이스에 간단한 파일 업로드(4)
이전 섹션에서는 여러 가지 사용자 정의 함수를 설정하고 새 파일 주소를 생성하는 방법을 구현했습니다.
여기서 전체 파일 업로드 기능을 완료하려면 사용자 정의 함수를 참조해야 합니다.
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>
새로운 파일
<?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>
시사
Clear
- 코스 추천
- 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~
이 강좌를 시청한 학생들도 학습하고 있습니다.
PHP로 사업을 시작하는 방법에 대해 간단히 이야기해 보겠습니다.
웹 프론트 엔드 개발에 대한 빠른 소개
민망한 물건 백과사전 사이트를 모방한 Mini 버전 MVC 프레임워크의 대규모 실용 Tianlongbabu 개발
PHP 실용 개발 시작하기: 빠른 PHP 생성 [중소기업 포럼]
로그인 인증 및 클래식 게시판
컴퓨터 네트워크 지식 수집
빠른 시작 Node.JS 정식 버전
당신을 가장 잘 이해하는 프론트엔드 강좌: HTML5/CSS3/ES6/NPM/Vue/...[원본]
자신만의 PHP MVC 프레임워크 작성(깊이 있는 40개 장/자세한 내용/초보자가 발전하려면 읽어야 함)
















