wordpress file upload from direct directory not working

I want to upload files from direct directory. But in my codes it will open media library then select files to upload

jQuery('#t_pdf').on("click", function(){

// alert("got it");

    var image = wp.media({

        title: "Upload PDF for Assignments",

        multiple: false

    }).open().on("select",function(){

        var uploaded_image = image.state().get("selection").first();

        var getpdf = uploaded_image.toJSON().url;

        jQuery("#show-pdf").html("<a class='btn btn-success' target='_blank' href='"+getpdf+"'>View PDF</a>");

        jQuery("#sub_file_pdf").val(getpdf);

    });

});

When I click button

<input type="button" class="btn btn-info" id="t_pdf" value="Upload File" required>
<span id="show-pdf"></span>
<input type="hidden" id="sub_file_pdf" name="sub_file">

it will follow the script and open media library, want to upload files from direct files directory and upload it to specific custom folder.

Can anyone help me to find out this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I fix this problems with wordpress file upload method:

php file:

add_action( 'wp_ajax_file_upload', 'file_upload_callbacks' );
add_action( 'wp_ajax_nopriv_file_upload', 'file_upload_callbacks' );


function file_upload_callbacks() {
    
    $arr_img_ext = array('application/pdf');
 if (in_array($_FILES['file']['type'], $arr_img_ext)) {
     $upload = wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));
     //$upload['url'] will gives you uploaded file path

     //var_dump($upload['url']);

     wp_send_json( $upload['url'] );
 }
 wp_die();
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x