1

我在上传文件时遇到了麻烦,下面的代码无论如何都应该回显一些内容,对吗?但是在提交表单后,尽管发送了邮件,但我没有收到任何消息。谁能帮我解决这个问题?

if(!empty($_FILES['file'])){

    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_mail($emailTo, $subject, $body, $headers, $attachments);
            #$hasError = true;
    }   

更新
我已经稍微改变了代码,它是如何进行的:

if (!function_exists('wp_handle_upload'))
{ 
     require_once( ABSPATH . '/wp-admin/includes/file.php' );
}
    
$uploadedfile = $_FILES['attachments'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
$attachments = $movefile;
if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.\n";
        #var_dump($movefile);
} else {
    echo "Possible file upload attack!\n";
}
wp_mail($emailTo, $subject, $body, $headers, $attachments);   

但我得到了这个错误

文件有效,已成功上传。异常“phpmailerException”,带有消息“无法访问文件:文件为空。请上传更实质性的内容。此错误也可能是由于您的 php.ini 中禁用上传或 post_max_size 被定义为小于 php.ini 中的 upload_max_filesize 造成的。

我试图解决这个问题

  • 更改上传限制没有帮助(我只能在 cpanel 执行此操作,并且这些设置由服务器管理员配置)
  • 上传额外的 php.ini 文件以增加限制也无济于事
4

2 回答 2

1
if (!function_exists('wp_handle_upload'))
{ 
     require_once( ABSPATH . '/wp-admin/includes/file.php' );
}

$uploadedfile = $_FILES['attachments'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
$attachments = $movefile;
if ( empty($movefile['error']) ) {
    echo "File is valid, and was successfully uploaded.\n";
    wp_mail($emailTo, $subject, $body, $headers, $attachments['file']); 
} else {
    echo "Possible file upload attack!\n";
}  

$attachments返回的是一个包含文件、url 和类型的数组。您只需要该文件作为附件。

于 2014-05-24T15:16:54.720 回答
0

在发送电子邮件之前尝试停止脚本

if(!empty($_FILES['file'])){

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);
                        exit();
                    } else {
                        echo "Possible file upload attack!\n";
                        exit();
                    }
                wp_mail($emailTo, $subject, $body, $headers, $attachments);
        #$hasError = true;
}

并查看是否有任何错误显示

于 2014-05-24T14:20:08.347 回答