0

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

2
  • so you just need to save the image in different table. is it a wp table or your custom created table can you please tell. And if possible could you more explain about the result like what you want in wp?
    – Prits
    Commented Apr 26, 2022 at 12:22
  • @Prits I have set up a separate table specifically for handling these files - they may be image files but may be pdf/doc/docx files so are not limited to just being image files. The table structure is as follows: fileID int(11) Auto Increment userID int(11) cpd_data_id int(11) fileName varchar(256) fileMime varchar(128) fileSize int(20) dateUploaded datetime file mediumblob This data interacts specifically with the plugin I am writing, and doe snot interact anywhere else inside the site. It's one of three tables made to handle data for the plugin. Commented Apr 26, 2022 at 12:42

1 Answer 1

1

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!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.