uploads - What is the hook to obtain the path and the name of the file that is being uploaded?

admin2025-06-05  3

I work in a plugin in which I need to obtain information of the current file that is uploaded. To this end, in the plugin I use a filter as follows.

add_filter('wp_handle_upload', 'custom_handle_upload', 10, 1);

function custom_handle_upload($upload) {
    $file = $upload['file'];
    $type = $upload['type'];

    if (hasValidType($type) && hasValidName($file)) {
        parseXLSX($file);
    }
}

If the file has the valid type and the name is correct, then it is processed. Otherwise the import should follow the normal flow.

But it is not the case, the file is uploaded to the upload directory but no data about the file is added to the database.

So I end up with broken references to the file.

When debugging the process and get to the function media_handle_upload located in wp-admin/includes/media.php I discover that the value of $file is null

$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);

If I deactivate the plugin, the import works as expected.

In order to solve, first try to change filter to action but the same thing happens. At this moment I think I should use another hook. I do not know what it could be and I do not know if it is correct.

What is the hook to obtain the path and the name of the file that is being uploaded?

I appreciate your opinion

Update: Actual solution

When uploading, the function media_handle_upload located at wp-admin/includes/media.php is invoked and at some point it call wp_handle_upload method. This method on success, returns an associative array of file attributes, so from the custom method I return the uploaded file. At the moment it looks like this

function custom_handle_upload($upload) {
    $file = $upload['file'];
    $type = $upload['type'];

    if (hasValidType($type) && hasValidName($file)) {
        parseXLSX($file);
    }

    return $upload;
}

And this allow me to upload common, and target files with it data

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749086642a316247.html

最新回复(0)