如何使用PHP在Mysql数据库中上传图像

时间:2017-02-14 10:13:28

标签: php mysql

我在使用PHP上传图像并在Mysql数据库中保存记录时遇到了麻烦,有人可以帮帮我吗?

代码:

<?php
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
?>
<html>
  <body>
    <form method="post" enctype="multipart/form-data">
      </br>
      <input type="text" name="dbname"/>
      <input type="file" name="dbimage"/>
      <br> <br>
      <input type="submit" name"submit" value"Upload"/>
    </form>
    <?php
    if(isset($_POST['submit'])){

      if(getimage($_FILES['dbimage']['tmp_name']) == FALSE) {
        echo "Please select an image";
      } else {
        $dbimage = addcslashes($_FILES['dbimage']['tmp_name']);
        $dbname = addcslashes($_FILES['dbimage']['dbname']);
        $dbimage = file_get_contents($image);
        $dbimage = base64_encode($dbimage);
        saveimage($dbname, $dbimage);
      }

      function saveimage() {
        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("db_test", $con);
        $qry = "insert into table1 (dname,dpic) values ('$dbname','$dbimage')";
        $result = mysql_query($qry, $con);
        if ($result){
          echo "Image uploaded.";
        } else {
          echo " Image not uploaded.";
        }
      }
    }?>
  </body>
</html>

3 个答案:

答案 0 :(得分:0)

你必须给函数saveimage()提供变量$ dbname和$ dbimage ...尽管如此,请使用PDO ......

答案 1 :(得分:0)

你在函数saveimage()中遗漏了参数。它应该是这样的: function saveimage($ dbname,$ dbimage)

答案 2 :(得分:0)

我认为你忘记了将params传递给你的函数

function saveimage($dbname,$dbimage){...}

尝试在您的条件之外宣布您的功能。