0

I am attempting to retain the original file name of my uploaded file using wp_handle_upload() and wp_mail().

Currently, all files are retaining their original file extension, but renaming themselves "file.ext". My $_FILES["image"]["name"] does still contain the correct file name.

I've tried adding a sanitization hook and creating an overwrites array using the correct file name, but clearly I am missing something...

<form id="requestcreditform" enctype="multipart/form-data">
  <input type="file" name="file" id="file" required>
</form>
<script>
(function($){
  $('#requestcreditform').submit( function(event) {
    event.preventDefault();
    document.getElementById("submit_button").disabled = true;
    
    var endpoint = '<?php echo admin_url('admin-ajax.php');?>';
   
    var form = $('#requestcreditform').serialize();
    var formdata = new FormData;
    
    formdata.append('action', 'requestcredit');
    formdata.append('requestcredit', form);
    formdata.append('image', document.getElementById("file").files[0]);
    
    $.ajax(endpoint, {
      type: 'POST',
      data: formdata,
      processData: false,
      contentType: false,
 
      success:function(res) {
        alert(res.data);
        location.reload();
      },
      error: function(err) {
      }
    })
  })
})
(jQuery)
</script>

In functions.php

add_action("wp_ajax_requestcredit", "request_credit1");
function request_credit1() {
  $image = $_FILES["image"];
  $imagename = $image['name'];

  $upload = wp_handle_upload($image, array('test_form' => false));

  try {
    if (wp_mail($recipient, $subject, $email_content, $headers, $upload)) {
      wp_send_json_success('Attempt sent.  Image name = '.$imagename);
    }
    else {
      wp_send_json_success('fail');
    }
  } catch (Exception $e) {
      wp_send_json_success($e->getMessage());
    }

Edit: The files are being uploaded to the correct name in my directory. Perhaps the culprit is wp_mail()?

2
  • Do I understand correctly, at some point the file has a filename that's 'correct' - i.e. the filename in $_FILES is correct and you can access the uploaded file using it, then at some point later the file gets renamed to literally "file.ext", and you're trying to figure out where that's happening? If incorrect, can you add some clarity to what your question is?
    – mozboz
    Commented Jul 4, 2023 at 11:54
  • @mozboz I was able to resolve the issue by pointing wp_mail to a slightly different location. After a night's sleep, I was able to better wrap my head around what was going on between the calls - a day's worth of banging my head against the wall wasn't doing me any favors. I was able to resolve the issue by pointing the wp_mail attachment to.. $attachment = $upload['file']; Commented Jul 6, 2023 at 18:00

1 Answer 1

0

I was able to resolve the issue by redirecting the wp_mail() attachment to...

$attachment = $upload['file'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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