无法上传文件

时间:2014-05-24 13:56:49

标签: php wordpress upload

我在上传文件时遇到了问题,以下代码在任何情况下都会回应一下吗?但是在提交表单之后,虽然邮件已发送,但我没有收到任何消息。任何人都可以帮我解决这个问题吗?

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中的上传被禁用或者被定义为小于php.ini中的upload_max_filesize的post_max_size引起的。'
更改上传限制没有帮助(我只能在cpanel上执行此操作并且这些设置由服务器的管理员配置),上传额外的php.ini文件以增加限制并没有帮助

2 个答案:

答案 0 :(得分: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是一个包含文件,网址和类型的数组。您只需要附件文件。

答案 1 :(得分: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;
}

并查看是否显示任何错误

相关问题