old php developer here, very.. very new to WP, so be kind.
As part of a plugin, I am trying to allow files to be uploaded with the intended destination storage in a specific table. I have the following code which works fine (almost).
// File handing preparation. Count how many files were uploaded
$wx_countFiles = count($_FILES['file']['name']);
// For each uploaded file, process them accordingly
for($i=0; $i<$wx_countFiles; $i++) {
// Assign values required
$wx_fileName = $_FILES['file']['name'][$i];
$wx_fileMime = $_FILES['file']['type'][$i];
$wx_fileSize = $_FILES['file']['size'][$i];
$wx_tmpf = $_FILES['file']['tmp_name'][$i];
$wx_file = fopen($_FILES['file']['tmp_name'][$i], "rb");
// Setup out allowed filetypes here
$wx_allowed_filetypes = array('doc', 'docx', 'pdf', 'png', 'jpg', 'jpeg', 'gif');
// Perform error/size checking
$wx_upload_error = null;
if(!in_array(strtolower(end(explode('.', $_FILES['file']['name'][$i]))), $wx_allowed_filetypes)) { $wx_upload_error .= "This filetype is not permitted<br>"; }
if($wx_fileSize > 15360000) { $wx_upload_error .= "File is too large<br>"; }
if($wx_fileSize == 0) { $wx_upload_error .= "The file is corrupted - 0 bytes<br>"; }
if($_FILES['file']['error'][$i] != UPLOAD_ERR_OK) { $wx_upload_error .= "There was an upload error<br>"; }
if($wx_upload_error == null) {
//$wpdb->query("INSERT INTO $tbl_docs(userID, dataID, fileName, fileMime, fileSize, dateUploaded, file) VALUES('$user_id', '$wx_dataID', '$wx_fileName', '$wx_fileMime', '$wx_fileSize', '$record_date', '$wx_file')");
}
The $wx_dataID value is set just a fraction earlier on in the process via a $wx_dataID = $wpdb->insert_id; step so that all the files associated with the upload are connected to entries in another table.
On post, all fields are filled correctly except the file input, which seems to just contain "Resource id #741" instead of the binary file within the assigned mediumblob cell. I have this system working perfectly using PDO prepare statements on another, non-WP system. I know WP prefers to utilise it's uploads director for this sort of thing, however for sync reasons these files need to be saved into the DB, not into the FS. Can anyone turn some light on how to do this for me? Thanks!
Working on DevKinsta, MariaDB, WP 5.9.2