0

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. how am i suppose to do that. thank you for your response!

2 Answers2

0

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...

Patrick
  • 104
  • 1
  • 12
0

First of all, you're looking for $_FILES['file']['tmp_name'], which is the file holding the file 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, i.e. the file contents. So all you need to do to get the file "BLOB" data is:

$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.

deceze
  • 510,633
  • 85
  • 743
  • 889