图像/文件上传到 mysql 数据库不起作用


Images/file upload to mysql database not working

我目前在将图像上传到我的数据库时遇到了一些困难。我目前从一个表单上传了多个变量/输入 - 如果这些输入是图像文件上传,则一个。该文件似乎让它做数据库,但是当我尝试通过PHP脚本检索图像时,它只返回"Array",而不是图像。有什么帮助吗?谢谢!

这是上传代码:

               // 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 类型。

这是一张图片,所以你可以得到一些关于我所说的视觉效果:https://i.stack.imgur.com/4pHwB.png

试试这个教程

http://www.tizag.com/phpT/fileupload.php

将文件名放在变量上,然后将其插入数据库(我希望您知道如何从数据库中检索数据)

$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'] ) . '" />';

但我不建议您这样做,因为它会使您的数据库加载速度不快,因此请将图像保存到文件夹中,并且仅在数据库中保留名称

注意我从一个论坛上得到了这个代码,但不记得它的名字对不起

图像直接上传到数据库通常是一个非常糟糕的主意。原因是一段时间后,从数据库读取和上传文件会使数据库过载。

更好的解决方案是将图像上传到服务器上的文件夹,然后将文件名和位置保存在数据库中。