0

我目前在将图像上传到我的数据库时遇到了一些困难。我目前从一个表单上传了多个变量/输入 - 如果这些输入是图像文件上传,则为一个。该文件似乎使它成为数据库,但是当我尝试通过 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

4

3 回答 3

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

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

请注意,我从论坛获得了此代码,但不记得它的名称抱歉

于 2012-04-18T22:59:39.173 回答
1

试试这个教程

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

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

于 2012-04-18T23:42:04.600 回答
1

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

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

于 2012-04-19T05:31:22.620 回答