代码之家  ›  专栏  ›  技术社区  ›  barni

PHP上传文件到MySQL数据库

  •  0
  • barni  · 技术社区  · 8 年前

    我试图在mysql数据库中上传一个文件,但当我点击“上传”按钮时,返回了我的普通PHP代码。我不知道我的问题在哪里。。

    <html>
        <head>
          ...
        </head>
        <body>
            <form id="upload" action="./upload.php" method="POST" enctype="multipart/form-data">
                <input type="file" name="file">
                <input type="submit" value="Upload">
            </form>
        </body>
    </html>
    

    <? php
    
    if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)
    {
        $file_name = $_FILES['file']['name'];
        $file_size = $_FILES['file']['size'];
        $file_tmp = $_FILES['file']['tmp_name'];
        $file_type = $_FILES['file']['type'];
    
        $parser = fopen($file_tmp, 'r');
        $content = fread($parser, filesize($file_tmp));
        $content = addslashes($content);
        fclose($parser);
    
        if(!get_magic_quotes_gpc())
        {
            $file_name = addcslashes($file_name);
        }
    
        $user = "root";
        $host = "localhost";
        $pass = "";
        $db   = "filemeup";
    
        try 
        {
            $conn = new PDO("mysql:host=$host;dbname=$db;", $user, $pass);
        } 
        catch(PDOException $e) 
        {
            echo "Error: ".$e->getMessage();
            exit;
        }
        $conn->query("SET NAMES utf8");
    
        $query="INSERT INTO files (name, size, type, content)"."VALUES (:name, :size, :type, :content) ";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':name', $file_name);
        $stmt->bindParam(':size', $file_size);
        $stmt->bindParam(':type', $file_type);
        $stmt->bindParam(':content', $content);
        $stmt->execute();
    ?>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Chris Happy Roddy    8 年前

    将开始的PHP标记更改为 <?php 并在文件末尾添加一个花括号。(缺少您的右括号) if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)