Programatically upload a file to be stored inside blob field in database, NOT on filesystem

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 permittedbr; }
        if($wx_fileSize  15360000) { $wx_upload_error .= File is too largebr; }
        if($wx_fileSize == 0) { $wx_upload_error .= The file is corrupted - 0 bytesbr; }
        if($_FILES['file']['error'][$i] != UPLOAD_ERR_OK) { $wx_upload_error .= There was an upload errorbr; }

        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

Topic uploads php plugins Wordpress

Category Web


After some further experimenting and talking to a colleague, we found the answer (team effort!) altering how the file handler deals with the file content.

        // 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");
        $wx_file = base64_encode(file_get_contents($_FILES['file']['tmp_name'][$i]));

Basically replacing fopen() with file_get_contents() and base64 encoding the stream. This worked (finally, yay!)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.