0

将图像上传到目录中,似乎总是尝试上传“”,这没什么。而不是真实的图像。我在下面做什么。我知道值是“”,因为它插入到数据库中。

<li class="formProductImage">
<span>Image:</span>
<input type="file" name="productImage" /> 
</li>

和PHP:

if ($_FILES["productImage"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["productImage"]["error"] . "<br />";
    }
else
    {
    echo "Upload: " . $_FILES["productImage"]["name"] . "<br />";
    echo "Type: " . $_FILES["productImage"]["type"] . "<br />";
    echo "Size: " . ($_FILES["productImage"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["productImage"]["tmp_name"] . "<br />";
    $filename = mysql_real_escape_string($_FILES['productImage']['name']);

      $query = "UPDATE products SET image = '$filename' WHERE name = '$name'";
      $result = mysql_query($query);

        if (mysql_affected_rows() == 1) {
            // Show thank you message
            echo '<span style="color:green;">Your image was added to database correctly.</span>';

            if (file_exists("uploaded/" . $_FILES["productImage"]["name"]))
            {
                echo $_FILES["productImage"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["productImage"]["tmp_name"],
                "uploaded/" . $_FILES["productImage"]["name"]);
                echo "Stored in: " . "uploaded/" . $_FILES["productImage"]["name"];
            }

        } else {
            echo '<font color="red">Image note inserted into database.</font>';
            echo mysql_error();
        }

    }
                    }

                        }
4

2 回答 2

2

确保你有enctype="multipart/form-data"标签<form>

于 2012-04-28T17:54:43.737 回答
1

改变这一行:

$filename = mysql_real_escape_string($_FILES['file']['name']);

到 :

$filename = mysql_real_escape_string($_FILES['productImage']['name']);
于 2012-04-28T17:45:56.043 回答