$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>uploads - What is the hook to obtain the path and the name of the file that is being uploaded?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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

Post a comment

comment list (0)

  1. No comments so far