简体   繁体   English

获取图像的BLOB值并将其插入php中的数据库

[英]get the BLOB value of an image and insert it to database in php

I have this code: 我有以下代码:

$filename = $_FILES['file']['name'];
$blob_value = [CODE TO CONVERT FILENAME TO BINARY];

$query = "INSERT INTO uploads VALUES('$blob_value');";
mysql_query($query);

I want to convert the image file to its equivalent blob_value and insert it to database. 我想将图像文件转换为其等效的blob_value并将其插入数据库。 how am i suppose to do that. 我应该怎么做。 thank you for your response! 感谢您的答复!

Maybe something like this is what you need 也许您需要这样的东西

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen( $tmpName, 'r' );

$content = fread( $fp, filesize( $tmpName ) );

fclose( $fp );

$query = "INSERT INTO upload ( name, size, type, content ) ".
"VALUES ( '$fileName', '$fileSize', '$fileType', '$content' )";

mysql_query( $query ) or die( 'Error, query failed' ); 

Theres probably lots of different ways to do this though, i haven't tried this code... 虽然可能有很多不同的方法可以执行此操作,但我尚未尝试过此代码...

First of all, you're looking for $_FILES['file']['tmp_name'] , which is the file holding the file data . 首先,您正在寻找$_FILES['file']['tmp_name'] ,它是保存文件data文件 Just the file name itself isn't of much use. 只是文件名本身没有多大用处。

Secondly, the "BLOB value" of a file is just the binary data of the file, ie the file contents. 其次,文件的“ BLOB值”只是文件的二进制数据,即文件内容。 So all you need to do to get the file "BLOB" data is: 因此,获取文件“ BLOB”数据所需要做的就是:

$blob = file_get_contents($_FILES['file']['tmp_name']);

Third, storing large files is not really what a database was designed for. 第三,存储大文件并不是数据库的真正目的。 You should store the file in the file system and only information about the file in the database. 您应该将文件存储在文件系统中,而仅将有关文件的信息存储在数据库中。 If you want to check whether you already have the exact same file, store a hash of the file in the database, which you can easily search for. 如果要检查是否已经有完全相同的文件,请将文件的哈希存储在数据库中,您可以轻松搜索该哈希

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM