I have a front-end form that is supposed to allow the user to upload a CV which is then sent along with the rest of the form as an attachment. To do so I have used wp_handle_upload() like so:

if ($_FILES) {

if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$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);
    } else {
        echo "Possible file upload attack!\n";

The form is then sent with wp_mail after checking for errors: ($receiver_email, $subject, $body, $headers are all fine and can be seen here

$attachments = $uploadedfile;

if (wp_mail($receiver_email, $subject, $body, $headers, $attachments)) {

    $jobs_email_sent = true;

} else {
    $jobs_email_sent_error = true;

However I am getting the following from var_dump($movefile) when I submit the form.

File is valid, and was successfully uploaded.
array(1) {
 string(212) "File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini."

And natually no attachment with the email. I checked php.ini and file_uploads is indeed on and upload_max_filesize is 128M. I have tried to add a pdf and a jpg file which are both quite small.

I would greatly appreciate any insight as to what I'm doing wrong here.

UPDATE just read that for wp_mail() the file path has to be fully specified as in $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip'); which should be returned in wp_handle_upload(); as file.

When I var_dump($_FILES) I get:

array(1) {
  array(5) {
    string(5) "3.jpg"
    string(10) "image/jpeg"
    string(14) "/tmp/php3aeSzD"

My file is named 3.jpg.

  • when you var_dump your $_FILES, do you get proper array? – Ivan Hanák Nov 13 '13 at 16:08
  • @IvanHanák yes see above. – mantis Nov 13 '13 at 16:19
  • 1
    So, according to the $_FILES array, you have your file name-d upload_file, albeit in your script you are getting something utterly different $uploadedfile = $_FILES['file']; – Ivan Hanák Nov 13 '13 at 16:33
  • Wow, I completely missed that. I'm still not attaching it properly though. I should double test that first. – mantis Nov 13 '13 at 16:39

Untested but I am pretty sure this is the issue:

You are trying to attach $uploadedfile to the email. You've set $uploadedfile to $_FILES['file']. That is a very temporary file/file location.

Once you use wp_handle_upload you want to be using $movefile instead. If you look at the Codex entry for that function you can see that you need (probably) $movefile['file'])

  • Still getting the same error. I tried $movefile on its own as well. – mantis Nov 13 '13 at 15:21
  • I am sure this is in the right direction. You will need to edit your question with more detail and probably more code. As is, I can't duplicate the whole process. – s_ha_dum Nov 13 '13 at 15:25
  • I attached the whole thing in a gist here: gist.github.com/mantismamita/7446379 – mantis Nov 13 '13 at 15:41
  • You were right. It was $movefile['file']. It also helped when I used the right name for the file ;) – mantis Nov 13 '13 at 17:08

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.