文件上传空字符串

时间:2012-04-28 17:43:18

标签: php mysql file-upload

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

<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();
        }

    }
                    }

                        }

2 个答案:

答案 0 :(得分:1)

更改此行:

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

到:

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

答案 1 :(得分:1)

确保enctype="multipart/form-data"代码中有<form>