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()?
$attachment = $upload['file'];