0

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?

0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.