我目前在将图像上传到我的数据库时遇到了一些困难。我目前从一个表单上传了多个变量/输入 - 如果这些输入是图像文件上传,则为一个。该文件似乎使它成为数据库,但是当我尝试通过 PHP 脚本检索图像时,它只是返回“数组”,而不是图像。有什么帮助吗?谢谢!
这是上传代码:
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$projectname = htmlentities($_POST['projectname'], ENT_QUOTES);
$item = htmlentities($_POST['item'], ENT_QUOTES);
$description = htmlentities($_POST['description'], ENT_QUOTES);
$neededby = htmlentities($_POST['neededby'], ENT_QUOTES);
$shipping= htmlentities($_POST['shipping'], ENT_QUOTES);
$revisions = htmlentities($_POST['revisions'], ENT_QUOTES);
$price = htmlentities($_POST['price'], ENT_QUOTES);
$paid = htmlentities($_POST['paid'], ENT_QUOTES);
$ordered1 = htmlentities($_POST['ordered1'], ENT_QUOTES);
$ordered2 = htmlentities($_POST['ordered2'], ENT_QUOTES);
$ordered3 = htmlentities($_POST['ordered3'], ENT_QUOTES);
$received1 = htmlentities($_POST['received1'], ENT_QUOTES);
$received2 = htmlentities($_POST['received2'], ENT_QUOTES);
$received3 = htmlentities($_POST['received3'], ENT_QUOTES);
$shipped1 = htmlentities($_POST['shipped1'], ENT_QUOTES);
$shipped2 = htmlentities($_POST['shipped2'], ENT_QUOTES);
$shipped3 = htmlentities($_POST['shipped3'], ENT_QUOTES);
$tracking = htmlentities($_POST['tracking'], ENT_QUOTES);
$delivered = htmlentities($_POST['delivered'], ENT_QUOTES);
$thestatus = htmlentities($_POST['thestatus'], ENT_QUOTES);
$photo=($_FILES['photo']);
if ($projectname == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in project name!';
renderForm($projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $thestatus, $photo, $error, $id);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT todo (projectname, item, description, neededby, shipping, revisions, price, paid, ordered1, ordered2, ordered3, received1, received2, received3, shipped1, shipped2, shipped3, tracking, delivered, photo, thestatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
$stmt->bind_param("sssssssssssssssssssss", $projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $photo, $thestatus);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: main.php");
}
}
文件检索代码:
<?php
mysql_connect("localhost","MYUSER","MYPASS");
mysql_select_db("MYDB");
$query = "SELECT photo FROM todo where id=$id";
$result = MYSQL_QUERY($query);
$data = MYSQL_RESULT($result,0,"photo");
Header( "Content-type: $type");
print $data;
?>
mysql 列是 BLOB 类型。
这是一张图片,因此您可以了解我所说的内容:http: //i.imgur.com/DYHHx.png