I created a form to send a letter with the ability to attach a file

here is the code

require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); $uploadedfile = $_FILES['file']; $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); if ( $movefile && !isset( $movefile['error'] ) ) { echo "File is valid, and was successfully uploaded.\n"; var_dump( $movefile); } else { echo $movefile['error']; } <input type="file" id="file" name="file" /> 

after sending I get this error

File is empty. Please upload something more substantial. This couldn’t have been your php.ini or post_max_size.

I checked the php.ini settings, file uploads are not disabled and do not exceed the size. through the admin section I upload files without problems

  • var_dump($_FILES); at the beginning of the file receiving script and see what is in the array and where the data will be taken from. - Visman pm
  • enctype='multipart/form-data' added to the enctype='multipart/form-data' form? - StasHappy
  • oh shit, I'm stupid: (((forgot to add enctype = 'multipart / form-data', everything works - akasergej
  • First says: File is empty - The file is empty. So the file did not reach the server or reached, but not completely. You can look at the error code: $ _FILES ['file'] ['error'] and find out more specifically which of these misunderstandings occurred. - StasHappy
  • @stashappy, the php code must be run by condition, rather than directly sculpted before html output. In all the examples I see this spelling. - Visman pm

1 answer 1

since I have never found the complete code for sending an email with wp_mail, I post my working code. can someone come in handy:

 require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); $uploadedfile = $_FILES['file']; $upload_overrides = array( 'test_form' => false ); $fileupload = wp_handle_upload( $uploadedfile, $upload_overrides ); if ( $fileupload ) { $attachments = $fileupload[ 'file' ]; } $headers = "From: ".$email."\r\n"; $headers.= "MIME-Version: 1.0\r\n"; $headers.= "Content-Type: text/html; charset=utf-8\r\n"; $headers.= "X-Priority: 1\r\n"; wp_mail($to, $subject, $message, $headers,$attachments); 
  • It is useful to those who use WordPress. - StasHappy