MySQL:如何使用HEX将文件存储到中等blob字段二进制文件中

I have wrote a simple VFS (Virtual File System) in PHP (StreamWrapper) that puts it's data into a MySQL database. The field that stores the data is a medium blob.

Things you can do for example in PHP:

copy( 'mytext.txt', 'dbfs://mytext.txt' );
copy( 'mytext.pdf', 'dbfs://mytext.pdf' ); or visa versa/etc.

The problem is binary data such as a pdf to pass thru a SQL statement. Escaping, unquoting, base64 etc ruin the data or take too much memory (overhead), for example base64 takes 3x the space.

The best thing to do is, i think, is make it a hex string (only twice the space) that will be stored in the database as binary (no overhead). I have seen the HEX command of MySQL but can't get it to work like I want.

For example, the next statement does not store it as binary data into the blob:

UPDATE dbfs SET data=0xFF883838<very long string>FFA9999...... WHERE (fid=<number>)

The function I made:

private function writeFile( $fid, &$uData = null, $iFileSize = null )
{ 
 $sSql = 'UPDATE '.self::$dbTableNameFat.
         ' SET data="'.(($uData !== null)?('0x'.bin2hex($uData)):null).'"'.
     ',size="'.((int)$iFileSize).'" WHERE ( fid="'.$fid.'" );';
 return $this->writeQuery( $sSql ); 
}

The blob contains the data but in HEX format and not in binary. How can I achieve the behaviour I want?

Use a prepared query. I'll use PDO-style syntax.

private function writeFile($fid, &$uData = null, $iFileSize = null) {
    $stmt = $this->prepare(
        "UPDATE " . self::$dbTableNameFat .
        " SET data = :data, size = :size WHERE fid = :fid");
    $stmt->execute(array( 'data' => $uData,
                          'size' => $iFileSize,
                          'fid' => $fid
                        );
};

PDO documentation is here