我目前在将图像上传到我的数据库时遇到了一些困难。我目前从一个表单上传了多个变量/输入 - 如果这些输入是图像文件上传,则一个。该文件似乎使它成为数据库,但当我尝试通过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
答案 0 :(得分:1)
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
$fp = fopen($tmpName, 'r');
$photo = fread($fp, filesize($tmpName));
$photo = addslashes($photo);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//and here your insert query as i remember you can try it
HTML CODE:
<input type=\"file\" name=\"image\" />
and here is how you retrive it
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
但我不建议您这样做,因为它会使您的数据库加载速度不快,因此将图像保存到文件夹中,只保存数据库中的名称
请注意我从论坛获得此代码并且不要记住它的名字抱歉
答案 1 :(得分:1)
答案 2 :(得分:1)
将图像直接上传到数据库通常是一个非常糟糕的主意。原因是因为一段时间后,从数据库中读取和上载文件会使数据库过载。
更好的解决方案是将图像上传到服务器上的文件夹,然后将文件名和位置保存在数据库中。