我有一个前端表单,允许用户上传简历,然后将简历作为附件与表单的其余部分一起发送。为此,我使用了wp_handle_upload()
像这样:
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";
}
}
检查错误后,表单将与wp\\U邮件一起发送:(
$receiver_email, $subject, $body, $headers
都很好,可以看到
here$attachments = $uploadedfile;
if (wp_mail($receiver_email, $subject, $body, $headers, $attachments)) {
$jobs_email_sent = true;
} else {
$jobs_email_sent_error = true;
}
然而,我从
var_dump($movefile)
当我提交表格时。
File is valid, and was successfully uploaded.
array(1) {
["error"]=>
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."
}
而且邮件自然没有附件。我检查了php。ini和file\\u uploads确实已打开,upload\\u max\\u filesize为128M。我尝试添加一个pdf和一个jpg文件,这两个文件都很小。
如果你能了解我在这里做错了什么,我将不胜感激。
更新只需阅读wp_mail()
必须完全指定文件路径,如中所示$attachments = array(WP_CONTENT_DIR . \'/uploads/file_to_attach.zip\');
应该在wp_handle_upload();
作为文件。
当我var\\u dump($\\u文件)时,我得到:
array(1) {
["upload_file"]=>
array(5) {
["name"]=>
string(5) "3.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/php3aeSzD"
["error"]=>
int(0)
["size"]=>
int(53262)
}
}
我的文件名为3。jpg。