通过前端提交上传文件,并通过电子邮件转发数据

时间:2014-01-08 作者:amespower

我使用wp_mail 这将转发我的前端提交数据并将其发布到仪表板。我遇到的问题是将附件文件与该电子邮件一起转发。我发现这篇帖子很有帮助:File upload from front-end form (as attachment) not working

我只能通过电子邮件转发图像的urlOR 将其发布到仪表板,但不能两者都发布。我真的需要它来做到这两个方面。问题是$movefile正是这样做的,将其从仪表板移动到电子邮件。有没有一种方法既可以将其保存在仪表板中,又可以转发该电子邮件?

    //successfully uploads file to dashboard
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file, $pid);
    }


    //successfully sends file url 
    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";
    } 

}

1 个回复
SO网友:amespower

如果有人需要,以下是我的解决方案:

if ($_FILES) {

    function insert_attachment($file_handler, $post_id, $setthumb = \'false\') {
    if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
    require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
    require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

    $attach_id = media_handle_upload( $file_handler, $post_id );

    //get url
    $attachment_url = wp_get_attachment_url($attach_id);
    add_post_meta($post_id, \'_file_paths\', $attachment_url);

    $attachment_data = array(
    \'ID\' => $attach_id
    );

    wp_update_post($attachment_data);


    if ($setthumb) update_post_meta($post_id, \'_thumbnail_id\', $attach_id);
    return $attach_id;

}

    foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file, $pid);
        //getting the url for the new upload
        $attachment_url = wp_get_attachment_url($newupload);
    }

}

结束

相关推荐

Protect Uploads in Multisite

我正在使用WordPress multisite(3.3.1),并试图保护从某些博客上传的内容不被热链接和直接访问。这与How to Protect Uploads, if User is not Logged In?, 但我只想通过服务器上运行的脚本访问上传内容(即,只有服务器可以显示/提供上传服务),因此内容可以受到WordPress用户权限的保护。例如,我有一个名为40c的字段。jpg位于localhost/files/2011/07/40c。jpg;我想让文件显示出来only 在本地域上由HTML