Base64编码上传文件,然后保存在数据库中

时间:2022-12-02 21:29:30

I want to base 64 encode uploaded file then save this into Blob type column of a table in MySQL database.

我想基于64个编码上传文件然后将其保存到MySQL数据库中表的Blob类型列中。

I have tried built in PHP function base64_encode with file variable but this does not seem to work.

我已经尝试使用文件变量内置PHP函数base64_encode,但这似乎不起作用。

Is there a way we can do this?

有没有办法可以做到这一点?

The reason is that I do not want to use moveuploaded file into a folder.

原因是我不想将moveuploaded文件用于文件夹。

Thank you.

1 个解决方案

#1


20  

As @Sjoerd and @zerkms correctly point out, you do not need to do this - a blob column is be used to store raw binary data, so you can bypass the Base64 process and insert the raw data of the file.

正如@Sjoerd和@zerkms正确指出的那样,您不需要这样做 - blob列用于存储原始二进制数据,因此您可以绕过Base64进程并插入文件的原始数据。

If you want to store images in a database (which, by the way, I personally don't like to do) it is better to store the raw data - Base64 encoding the data makes it larger, and means that it must be decoded before the image is rendered, which adds processing overhead.

如果你想将图像存储在数据库中(顺便说一下,我个人不喜欢这样做),最好存储原始数据 - 对数据进行Base64编码使其更大,并且意味着它必须先解码渲染图像,这增加了处理开销。

This is how you can insert the raw binary data (assuming the MySQL extension):

这是你如何插入原始二进制数据(假设MySQL扩展):

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`blob_column`)
  VALUES
    ('$data')
";

mysql_query($query);

If you really do want to Base64 encode it (in which case it could just be stored in a varchar), just just add a base64_encode() call:

如果你真的想对它进行Base64编码(在这种情况下它可以存储在varchar中),只需添加一个base64_encode()调用:

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = base64_encode($data);

// There is an argument that this is unnecessary with base64 encoded data, but
// better safe than sorry :)
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`varchar_column`)
  VALUES
    ('$data')
";

mysql_query($query);

#1


20  

As @Sjoerd and @zerkms correctly point out, you do not need to do this - a blob column is be used to store raw binary data, so you can bypass the Base64 process and insert the raw data of the file.

正如@Sjoerd和@zerkms正确指出的那样,您不需要这样做 - blob列用于存储原始二进制数据,因此您可以绕过Base64进程并插入文件的原始数据。

If you want to store images in a database (which, by the way, I personally don't like to do) it is better to store the raw data - Base64 encoding the data makes it larger, and means that it must be decoded before the image is rendered, which adds processing overhead.

如果你想将图像存储在数据库中(顺便说一下,我个人不喜欢这样做),最好存储原始数据 - 对数据进行Base64编码使其更大,并且意味着它必须先解码渲染图像,这增加了处理开销。

This is how you can insert the raw binary data (assuming the MySQL extension):

这是你如何插入原始二进制数据(假设MySQL扩展):

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`blob_column`)
  VALUES
    ('$data')
";

mysql_query($query);

If you really do want to Base64 encode it (in which case it could just be stored in a varchar), just just add a base64_encode() call:

如果你真的想对它进行Base64编码(在这种情况下它可以存储在varchar中),只需添加一个base64_encode()调用:

$data = file_get_contents($_FILES['name_of_control']['tmp_name']);
$data = base64_encode($data);

// There is an argument that this is unnecessary with base64 encoded data, but
// better safe than sorry :)
$data = mysql_real_escape_string($data);

$query = "
  INSERT INTO table
    (`varchar_column`)
  VALUES
    ('$data')
";

mysql_query($query);