我正在编写一个小的Wordpress插件,该插件将提供联系表,提交表单后,将触发文件下载,并且需要显示确认消息。
问题是,我正在使用以下内容触发文件下载:
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
这将触发下载正常。但是,接下来我应该显示以下确认消息:
$output = '<div id="' . $unique_id . '" class="' . $css_class . '">';
$output .= $content;
if($a['provide_download_link'] == '1'){
$output .= '<p><a href="'.$file_url.'">Click here to download.</a></p>';
}
$output .= '</div>';
return $output;
但是,这不起作用,而是页面停留在表单显示的状态。 在“返回$ output”之前,我还尝试添加:
header('Content-Type: text/html');
希望它会在触发文件下载后更改标头Content-Type,然后显示确认消息的html。
如果我尝试在这两个操作之间切换,返回输出然后触发文件下载,则确认消息显示得很好,但是文件下载没有被触发。
这是我的完整代码:
if(isset($_POST['gd_fdf_submit']) && $_POST['gd_fdf_submit'] == $a['submit_label']){
if($_POST['gd_fdf_validation'] == ''){
$file_url = $a['file_url'];
$output = '<div id="' . $unique_id . '" class="' . $css_class . '">';
$output .= $content;
if($a['provide_download_link'] == '1'){
$output .= '<p><a href="'.$file_url.'">Click here to download.</a></p>';
}
$output .= '</div>';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
if($a['form_recipient'] == ''){
$recipient = get_option('admin_email');
} else {
$recipient = $a['form_recipient'];
}
if($a['from_email'] == ''){
$from = get_option('admin_email');
} else {
$from = $a['from_email'];
}
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = "From: " . $from;
if(isset($_POST['user_email']) && $_POST['user_email'] != ''){
$headers[] = "Reply-To: " . $_POST['user_email'];
}
$subject = $a['email_subject'];
$message = '';
if(isset($_POST['user_name']) && $_POST['user_name'] != ''){
$message .= '<strong>Name:</strong> ' . $_POST['user_name'] . '<br />';
}
if(isset($_POST['user_email']) && $_POST['user_email'] != ''){
$message .= '<strong>E-Mail:</strong> ' . $_POST['user_email'] . '<br />';
}
if(isset($_POST['user_phone']) && $_POST['user_phone'] != ''){
$message .= '<strong>Phone:</strong> ' . $_POST['user_phone'] . '<br />';
}
if(isset($_POST['user_message']) && $_POST['user_message'] != ''){
$message .= '<strong>Message:</strong> ' . $_POST['user_message'] . '<br />';
}
$message .= '<strong>File Downloaded:</strong> ' . $file_url;
mail($recipient, $subject, $message, implode("\r\n", $headers));
header('Content-Type: text/html');
return $output;
}
}
在这种情况下,文件下载被触发得很好,但随后不会显示确认消息。
或者:
if(isset($_POST['gd_fdf_submit']) && $_POST['gd_fdf_submit'] == $a['submit_label']){
if($_POST['gd_fdf_validation'] == ''){
$file_url = $a['file_url'];
$output = '<div id="' . $unique_id . '" class="' . $css_class . '">';
$output .= $content;
if($a['provide_download_link'] == '1'){
$output .= '<p><a href="'.$file_url.'">Click here to download.</a></p>';
}
$output .= '</div>';
return $output;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
if($a['form_recipient'] == ''){
$recipient = get_option('admin_email');
} else {
$recipient = $a['form_recipient'];
}
if($a['from_email'] == ''){
$from = get_option('admin_email');
} else {
$from = $a['from_email'];
}
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = "From: " . $from;
if(isset($_POST['user_email']) && $_POST['user_email'] != ''){
$headers[] = "Reply-To: " . $_POST['user_email'];
}
$subject = $a['email_subject'];
$message = '';
if(isset($_POST['user_name']) && $_POST['user_name'] != ''){
$message .= '<strong>Name:</strong> ' . $_POST['user_name'] . '<br />';
}
if(isset($_POST['user_email']) && $_POST['user_email'] != ''){
$message .= '<strong>E-Mail:</strong> ' . $_POST['user_email'] . '<br />';
}
if(isset($_POST['user_phone']) && $_POST['user_phone'] != ''){
$message .= '<strong>Phone:</strong> ' . $_POST['user_phone'] . '<br />';
}
if(isset($_POST['user_message']) && $_POST['user_message'] != ''){
$message .= '<strong>Message:</strong> ' . $_POST['user_message'] . '<br />';
}
$message .= '<strong>File Downloaded:</strong> ' . $file_url;
mail($recipient, $subject, $message, implode("\r\n", $headers));
}
}
请注意,这里我已将“返回$ output”移到“ header('Content-Type:application / octet-stream');”上方。等等。在这种情况下,确认消息显示得很好,但是文件下载没有被触发。
我哪里出错了?