程序在PHP中上传图像

时间:2014-08-06 11:08:45

标签: php mysql mysqli

我正在努力制作一个"上传"按钮使用PHP和HTML并将图像保存在mysql数据库中。使用mysqli,我意识到我的数据库连接是正确的,但是,我仍然得到相同的错误"问题上传图像。" 。谁能向我解释我做错了什么?

member.php

<form action="member.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image" /><input type="submit" value="Upload" />
</form>
<?php
$mysqli=mysqli_connect('localhost','root','root','users') or die("Database Error");

 $file = $_FILES['image']['tmp_name'];
 if (!isset($file))
    echo "Please select an image.";
 else
 {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size==FALSE)
        echo "That's not an image file.";
    else {
        if (!$insert = mysqli_query("INSERT INTO store VALUES ('','$image_name','$image')"))
         echo "Problem uploading image.";
        else {
            echo "Image uploaded.";
        }
    }

 }

?>

提前致谢。

此致 史蒂夫

3 个答案:

答案 0 :(得分:1)

我认为你必须先上传文件!

试试这段代码:

<?php
$mysqli=mysqli_connect('localhost','root','root','users') or die("Database Error");

 $file = $_FILES['image']['tmp_name'];
 if (!isset($file))
    echo "Please select an image.";
 else
 {
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['image']['name']);
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
          echo "File is valid, and was successfully uploaded.\n";

          $image = file_get_contents($uploadfile);
          $image_name = $_FILES['image']['name']);

          if (!$insert = mysqli_query("INSERT INTO store VALUES ('','$image_name','$image')"))
             echo "Problem uploading image.";
          else {
            echo "Image uploaded.";
           }



    } else {
         echo "Possible file upload attack!\n";
    }


 }

?>

答案 1 :(得分:1)

使用mysqli_error();来获取确切的错误。它将生成插入数据库时​​出现的错误。

<?php
$mysqli=mysqli_connect('localhost','root','root','users') or die("Database Error");

 $file = $_FILES['image']['tmp_name'];
 if (!isset($file))
    echo "Please select an image.";
 else
 {
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['image']['name']);
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
          echo "File is valid, and was successfully uploaded.\n";

          $image = file_get_contents($uploadfile);
          $image_name = $_FILES['image']['name']);
          $insert = mysqli_query("INSERT INTO store VALUES ('','$image_name','$image')") or die(mysqli_error());
          if (!$insert)
             echo "Problem uploading image.";
          else {
            echo "Image uploaded.";
           }

 } else {
         echo "Possible file upload attack!\n";
    }


 }

?>

如果您的查询中有任何错误,则会显示。现在很容易就可以删除错误,图片将被上传。

答案 2 :(得分:0)

尝试以下mysqli_query的语法:

$con=mysqli_connect('localhost','root','root','users');
mysqli_query ( $con, "INSERT INTO store VALUES ('','$image_name','$image')" );

将连接对象作为代码中的第一个参数传递。