• I am new to wordpress, but a customer wants one of my scripts converted into a wordpress plugin.

    The basic idea of the script is when a file is uploaded with a specific filetype, then start decoding it (private In-company filetype)

    Now, while it is being processed, is there a way to edit the “crunching….” text to display status of the decoding?

    Also, if the decode fails, then it CANT BE UPLOADED… is there a way to reject the file and stop the upload process?

    code:

    add_action('wp_handle_upload', 'filetype_check');
    
    function filetype_check($results) {
        if ($results['type'] == "D3m58") {
            Process($results['file']);
            $result2 = wordingcheck(textdecode($decodedtext);
            if ($result2 != "") {
                echo "upload failed, following reasons: <br>\n" . $result2;
                die();
            }
        }
    }

    The text should be placed inside the loading bar instead of the current Crunching.

    Instead of die(), I want the upload to abort.

    here is a image

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    First of all. ‘wp_handle_upload’ is a filter, not an action. Same hook concept as an action, but it’s added with add_filter(). When this filter runs, the file has already been uploaded. You could go ahead and unlink it, but you cannot interrupt and cancel anything at this point.

    I believe you could hook the ‘wp_handle_upload_prefilter’ and pre-fetch the 1st few kb or whatever is needed to verify the file before committing to a full download, but there’s no way to interrupt a download from WP that I know of.

    I’m not sure if the error messages you can return show up where you want, but they are the way to handle errors. From ‘wp_handle_upload_prefilter’ return the passed $file array with $file['error'] set to an error number corresponding to PHP $_FILES['userfile']['error'] errors, you cannot define your own.

    If you stick with ‘wp_handle_upload’, return the results of a call to call_user_func('wp_handle_upload_error', $file, 'Your error message').

    Thread Starter jmyeom

    (@jmyeom)

    ‘wp_handle_upload’ was working fine with add_action, although works fine with add_filter as-well, so i set it as a filter.

    we cant use ‘wp_handle_upload_prefilter’, as the file needs to be decoded as a whole, but thanks for the suggestion.

    call_user_func('wp_handle_upload_error', $results['file'], 'Your error message').

    gets the error:
    Warning: Parameter 1 to wp_handle_upload_error() expected to be a reference, value given

    Moderator bcworkz

    (@bcworkz)

    I guess I should have read the documentation for call_user_function(). I sort of copied the line from the wp_handle_upload() source, wrongly condensing 2 lines into one. Uncondensed version:

    $upload_error_handler = 'wp_handle_upload_error';
    call_user_func($upload_error_handler, $results['file'], 'Your error message');

    Sorry about that.

    As far as actions vs. filters, I just discovered they are interchangeable. In the past they may have been different, but no more. And all this time I’ve been meticulously ensuring I add my hooks to the correct version, when it all gets added to the same place either way. Sigh.

    Thread Starter jmyeom

    (@jmyeom)

    i got no joy with your code, same error…

    i dont think that was the issue, more that the issue was with :$results['file'] ,

    also, wp_handle_upload_error does nothing more then return a array,

    but that array is return by wp_handle_upload

    i have been searching through the source code of wordpress to find a way to editing the crunching, and it seems the best way of doing that is by causing the upload to fail with a error message

    i am sat here with everything in debug mode to find the best way to fail this upload……i have had no joy so far…

    if anyone has any ideas, it would be a big help

    Thread Starter jmyeom

    (@jmyeom)

    so…. after a lot of hunting, a lot of source code exploring, this is the most effective way of doing it:

    add_action('wp_handle_upload', 'filetype_check');
    
    function filetype_check($results) {
        if ($results['type'] == "D3m58") {
            Process($results['file']);
            $result2 = wordingcheck(textdecode($decodedtext);
            if ($result2 != "") {
                unlink($results['file']);
                return array( 'error'=> "upload failed, following reasons: " . $result2);
    
            }
        }
    }

    I hope this helps someone, and here is a basic overview of what is happening:

    you cant stop the upload, the file is uploaded before it hits the plugins, i have searched the code for a hook that is called earlier to stop it, but there is not one

    to show a error, you just need to return an array with error and the message:
    return array( 'error'=> "The error");

    here is a image of the final result (and yes, the file is really deleted)

    hope it helps!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘can a plugin stop a upload?’ is closed to new replies.