CF7表单文件根据下拉选择上传到FTP服务器

时间:2017-05-01 01:47:15

标签: php wordpress ftp contact-form-7

我在联系表格7中设置了一个简单的请求表单,其中包含两个设施的下拉选项。根据选择的下拉设施,有两个匿名服务器地址可以上传文件。我已成功将以下代码添加到functions.php文件中,对其进行了测试,并且对于其中一个下拉选项,它可以正常工作。我的问题是,我无法为其他下拉列表选择正确的代码上传和正常工作。

设置是表单部分转到分布式电子邮件地址,文件上传到Anaheim或Orange FTP服务器。

ftp **。domain.com - Orange Facility ftp **。domain.com - Anaheim Facility

这仅适用于Orange Facility。现在我似乎无法为第二个代码获得正确的逻辑。我尝试了两个独立的功能(FacilityA和FacilityB),但我仍然坚持如何采取" SelectAFacility"命名并将其拆分为2个函数。

[select SelectAFacility" Anaheim | ftpuploadanaheim@domain.com Orange|ftpuploadorange@domain.com"]

我在WordPress StackExchange上被告知这个问题不在话题,所以我在这里发布了。任何帮助都会很棒!

以下是代码:

add_action('wpcf7_before_send_mail', 'log_cf7');
function log_cf7($WPCF7_ContactForm) {

    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    $uploadedFiles = $submission->uploaded_files();

$location = $data['SelectAFacility'];

    if(isset($uploadedFiles) && isset($uploadedFiles['your-file'])){

$ftp_server="ftp**.domain.com";
$ftp_username="username";
$ftp_userpass="password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$fileToUpload =     $uploadedFiles['your-file'];
$remotefile = $_FILES['your-file']['name'];

    if (ftp_put($ftp_conn, $remotefile, $fileToUpload, FTP_ASCII))
 {
    //echo "Successfully uploaded $remotefile.";
  }
    else
 {
    // echo "Error uploading $fileToUpload.";
  }
   // close connection
   ftp_close($ftp_conn);
 }
 }

1 个答案:

答案 0 :(得分:0)

所以只需添加一个基于SelectAFacility的值的简单条件语句:

if ($location == 'Facility A') {
        $ftp_server = "ftp**.domainA.com";
        $ftp_username = "username A";
        $ftp_userpass = "password A";
    } elseif ($location == 'Facility B') {
        $ftp_server = "ftp**.domainB.com";
        $ftp_username = "username B";
        $ftp_userpass = "password B";
    } else {
        // do something for unknown facility if you want
    }
}

所有在一起:

function log_cf7($WPCF7_ContactForm)
{
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    $uploadedFiles = $submission->uploaded_files();

    $location = $data['SelectAFacility'];

    if (isset($uploadedFiles) && isset($uploadedFiles['your-file'])) {

        if ($location == 'Facility A') {
            $ftp_server = "ftp**.domainA.com";
            $ftp_username = "username A";
            $ftp_userpass = "password B";
        } elseif ($location == 'Facility B') {
            $ftp_server = "ftp**.domainB.com";
            $ftp_username = "username B";
            $ftp_userpass = "password B";
        } else {
            // do something for unknown facility if you want
            exit();
        }

        $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to server");
        $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

        $fileToUpload = $uploadedFiles['your-file'];
        $remotefile = $_FILES['your-file']['name'];

        if (ftp_put($ftp_conn, $remotefile, $fileToUpload, FTP_ASCII)) {
            //echo "Successfully uploaded $remotefile.";
        } else {
            // echo "Error uploading $fileToUpload.";
        }
        // close connection
        ftp_close($ftp_conn);
    }
}