Home > Software engineering >  Pass global variable into function not working
Pass global variable into function not working

Time:08-01

I am trying to use allow my custom API endpoint to upload files to a custom directory based on information sent in the request body. It is coming through fine but I am not getting it to pass into the directory properly. I have tried getting the studentid from the request body and then calling that as a global in my function but it is not working.

add_filter("wcra_upload_callback", "wcra_upload_callback_handler");
function wcra_upload_callback_handler($request) {
    if (!function_exists('wp_handle_upload')) {
        require_once(ABSPATH.'wp-admin/includes/file.php');
    }
    $studentid = $request['studentid'];
    function studentresultsdir($dir) {
        global $studentid;
        $mydir = 'https://example.com/wp-content/uploads/studentresults/';
        $dir['path'] = $mydir;
        $dir['url'] = $mydir;
        $dir['subdir'] = $studentid;
        var_dump($dir);
        return $dir;
    }
    add_filter("upload_dir", "studentresultsdir");
    $uploadedfile = $_FILES['file'];
    $upload_overrides = array('test_form' => false);
    $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
    if ($movefile && !isset($movefile['error'])) {
        echo __('File is valid, and was successfully uploaded.', 'textdomain')."\n";
        var_dump($movefile);
    } else {
        echo $movefile['error'];
    }
    remove_filter("upload_dir", "studentresultsdir");
}

My var_dump of $dir is giving me an empty subdirectory. I think this is the cause of my "unable to create directory" error too but need to work this step out first.

Just wanted to add that I will be adding authentication checks once I get this working.

CodePudding user response:

I managed to solve this particular error by moving the global variable outside of all functions.

add_filter("wcra_upload_callback", "wcra_upload_callback_handler");
$studentid = '';
function wcra_upload_callback_handler($request) {
    if (!function_exists('wp_handle_upload')) {
        require_once(ABSPATH.'wp-admin/includes/file.php');
    }
    global $studentid;
    $studentid = $request['studentid'];
    function studentresultsdir($dir) {
        global $studentid;
        $mydir = 'https://example.com/wp-content/uploads/studentresults/';
        $dir['path'] = $mydir;
        $dir['url'] = $mydir;
        $dir['subdir'] = '/'.$studentid;
        var_dump($dir);
        return $dir;
    }
    add_filter("upload_dir", "studentresultsdir");
    $uploadedfile = $_FILES['file'];
    $upload_overrides = array('test_form' => false);
    $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
    if ($movefile && !isset($movefile['error'])) {
        echo __('File is valid, and was successfully uploaded.', 'textdomain')."\n";
        var_dump($movefile);
    } else {
        echo $movefile['error'];
    }
    remove_filter("upload_dir", "studentresultsdir");
}

CodePudding user response:

Replace this line global $studentid; with following line

global $studentid, $dir;

  • Related