使用 php 在 MySql 数据库中插入 Blob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7052655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:53:49  来源:igfitidea点击:

Insert Blobs in MySql databases with php

phpmysqlblob

提问by nikhil

I am trying to store an image in the DataBase, for some reason it doesn't seem to work. Here's the structure of my table.

我试图在数据库中存储图像,由于某种原因它似乎不起作用。这是我的表的结构。

mysql> describe ImageStore;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| ImageId | int(11)  | NO   | PRI | NULL    |       |
| Image   | longblob | NO   |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

And here is my query which inserts the image or at least thats what it should:

这是我插入图像的查询,或者至少这是应该的:

//Store the binary image into the database
                $tmp_img = $this->image['tmp_name'];
                $sql = "INSERT INTO ImageStore(ImageId,Image)               
                VALUES('$this->image_id','file_get_contents($tmp_image)')";
                mysql_query($sql); 

If I print the value of file_get_contents($tmp_image), then there is a tons of data on the screen. But this value doesn't get stored in the database and that is the issue that I'm facing.

如果我打印 file_get_contents($tmp_image) 的值,那么屏幕上就会有大量数据。但是这个值不会存储在数据库中,这就是我面临的问题。

回答by Lightness Races in Orbit

Problem

问题

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','file_get_contents($tmp_image)')";

This creates a string in PHP named $sql. Forget about MySQL for a minute, because you're not executing any query yet. You're just building a string.

这将在 PHP 中创建一个名为$sql. 暂时忘记 MySQL,因为您还没有执行任何查询。你只是在构建一个字符串。

The magic of PHP means that you can write a variable name — say, $this->image_idinsidethe double quotes and the variable still gets magically expanded.

PHP 的神奇之处在于你可以在双引号写一个变量名——比如,$this->image_id——并且变量仍然会被神奇地扩展。

This functionality, known as "variable interpolation", does not occur for function calls. So, all you're doing here is writing the string "file_get_contents($tmp_image)"into the database.

此功能称为“变量插值”,不会发生在函数调用中。因此,您在这里所做的就是将字符串"file_get_contents($tmp_image)"写入数据库。



Solution (1)

解决方案 (1)

So, to concatenate the result of calling file_get_contents($tmp_image), you have to jump out of the string and do things explicitly:

因此,要连接调用的结果file_get_contents($tmp_image),您必须跳出字符串并显式执行以下操作:

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','" . file_get_contents($tmp_image) . "')";

(You can see even just from the syntax highlighting how this has worked.)

(您甚至可以从突出显示这是如何工作的语法中看到。)



Solution (2)

解决方案(2)

Now the problem you have is that if the binary data contains any ', your query is not valid. So you should run it through mysql_escape_stringto sanitize it for the query operation:

现在您遇到的问题是,如果二进制数据包含 any ',则您的查询无效。所以你应该运行它mysql_escape_string来为查询操作清理它:

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";


Solution (3)

解决方案 (3)

Now you have a reallybig string, and your database is getting bulky.

现在您有一个非常大的字符串,并且您的数据库变得越来越庞大。

Prefer not storing images in databases, where you can help it.

最好不要将图像存储在可以帮助它的数据库中。

回答by Ben

To expand on Tomalak's comment, you can't run a function inside of quotes.

要扩展 Tomalak 的评论,您不能在引号内运行函数。

Try:

尝试:

$sql = "INSERT INTO ImageStore(ImageId,Image)               
        VALUES('{$this->image_id}','".file_get_contents($tmp_image)."')";

回答by classic

try this:

尝试这个:

$tmp_img = $this->image['tmp_name'];
$sql = "INSERT INTO ImageStore(ImageId,Image)               
  VALUES('$this->image_id','" . addslashes(file_get_contents($tmp_image)) . "')";
mysql_query($sql);

回答by Shahrokhian

As mentioned you are just saving the string "file_get_contents($tmp_image)"into the db but you need to run the function file_get_contentsinstead
dont forget to hash the image using a hashing algorithm such as base64_encodebefore saving it to the db.

如前所述,您只是将字符串保存"file_get_contents($tmp_image)"到数据库中,但您需要运行该函数,file_get_contents而不是
忘记使用哈希算法对图像进行哈希处理,例如base64_encode在将其保存到数据库之前。