I am attempting to INSERT a single file's data into a LONGBLOB column via PHP along with some other data like the file's name, size in bytes, sha256 checksum, etc.
I know there is nothing wrong with the table itself, as I am able to INSERT the file and the rest of the columns all at once through phpMyAdmin which suggests that I use the following MySQL INSERT statement:
INSERT INTO DB_NAME.TABLE_NAME (SHA256SUM, FILE_NAME, FILE_DATA) VALUES (?, ?, ?)
So my PHP code currently looks like this:
$mysqli_query = 'INSERT INTO DB_NAME.TABLE_NAME (SHA256SUM, FILE_NAME, FILE_DATA) VALUES (?, ?, ?)';
$stmt = $mysqli->prepare($mysqli_query);
$null = NULL;
$stmt->bind_param("ssb", $sha256sum, $_FILES['career_application_FILE_UPLOAD']['name'], $null);
$stmt->send_long_data(0, file_get_contents($_FILES['career_application_FILE_UPLOAD']['tmp_name']));
$stmt->execute();
Which is pretty much exactly what Oracle suggests right here.
But for some reason it just hasn't been working for me. (And trust me, I have been trying for the past 3 hours.)
It kept returning the general MySQL error "COLUMN FILE_DATA CANNOT BE NULL".
So I enabled NULL values on the FILE_DATA column, and then the INSERT would work, but the FILE_DATA column (which is supposed to be filled with BLOB data) just reads NULL.
So I disabled NULL values on the FILE_DATA column and I placed a $stmt_error = $stmt->error;
line just after the send_long_data section and I caught the following error:
Can't send long data for non-string/non-binary data types
I tried deciphering what that meant, and was unable to, so here I am asking for help.
I'm going to get some sleep, and hopefully in the morning I can answer my own silly question... I just have this feeling that it's something so entirely obvious I am too tired to notice it right now.
Some details:
- I am running this code on Debian 8 and NGINX v1.6.2
- I have quadruple-checked that my MySQL max_allowed_packet is set to the default 16776192 (which shouldn't matter anyways, as the files I am attempting to upload are a few bytes to a few kilobytes in size)
- I have attempted uploading all different types of files (txt, mov, png, pdf, etc)
- I am not unfamiliar with PHP or MySQL at all, I have been working with them both for over 10 years now
- I even tried setting the LONGBLOB collation to binary, since the entire table's collation was ascii... but it wasn't possible to change the LONGBLOB collation, nor should it matter since like I said originally... phpMyAdmin has no problem inserting file data into the LONGBLOB column)
Thanks for reading.