I'm trying to make a form where the user will be able to attach image file and then the image will be send on e-mail as attachment via wp_mail. Here is what I've done so far: The code for the attachment button:
<div class="attachment-row">
<form method="POST" enctype="multipart/form-data">
<input type="file" class="input-field" name="file">
</form>
</div>
The code for moving the image and sending it as attachment:
move_uploaded_file($_FILES["file"]["tmp_name"],WP_CONTENT_DIR .'/uploads/'.basename($_FILES['file']['name']))
$attachments = array(WP_CONTENT_DIR .'/uploads/'.$_FILES["file"]["name"]);
$mailResult = wp_mail( $to, $subject, $message, $headers, $attachments);
And here is how I receive the image ( blank file instead of image attachment ). I don't know what I'm doing wrong, any kind of help is welcomed. Thanks!
Edit - I've also tried this way(to save the file name into variable)but it still doesn't work.
$target_dir = WP_CONTENT_DIR . "/uploads/";
$image_name=$_FILES['file'['name'];
$target_file = $target_dir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
To note that when I check my /wp-content/uploads/ folder the images that I've attached is not there - I guess the image is not saved but I cant figure out why.
Edit again-I've tried this way too but it don't work again:
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
echo "noo";
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
$attachments = $movefile[ 'file' ];
$mailResult = wp_mail( $to, $subject, $message, $headers, $attachments);
} else {
}
and the html form:
<form method="POST" enctype="multipart/form-data">
<input type="file" class="input-field" name="file">
</form>
I'm stuck and I can't figure what I'm doing wrong...