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>
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!