我尝试使用php功能从websever下载文件。此函数是我附加到表单的脚本的一部分。我的目标是在用户点击提交按钮后,通过浏览器下载文件。我的表单代码将填写的所有数据发送到电子邮件地址,并通过确认通知向客户发送电子邮件,此代码有效并且没有任何已知错误。我可以使用以下php脚本从我的服务器下载文件:
<?php
$name = 'Automated_Drones.pdf';
$fp = fopen($name, 'rb');
header("Content-Type: application/pdf");
header("Content-Length: " . filesize($name));
fpassthru($fp);
?>
但是,当我尝试将此脚本放入函数并使用我的表单电子邮件脚本调用该函数时,它似乎无法正常工作。我可以创建任意脚本并将上面的代码放入一个函数并调用它,它工作正常。但是,当我使用我的表单电子邮件脚本尝试它时它根本不起作用。我使用我的所有脚本记录错误,并且没有出现我目前所拥有的错误。电子邮件会转到表单上的地址,第二个电子邮件地址会收到他的详细信息,所以我确定这不是问题。这是我的最新剧本,我一直在努力工作:
<?php
global $_REQUEST, $wpdb;
$response = array('error'=>'');
$user_exp = test_input($_REQUEST['user_exp']);
$user_name = test_input(substr($_REQUEST['user_name'], 0, 20));
$user_surname = test_input($_REQUEST['user_surname']);
$user_title = test_input($_REQUEST['user_title']);
$user_industry = test_input($_REQUEST['user_industry']);
$user_email = test_input(substr($_REQUEST['user_email'], 0, 40));
$user_phone = test_input($_REQUEST['user_phone']);
//Download functions are run here, i comment out the functions im not using
//downloadFile();
//curl_get_file_contents('Automated_Drones.pdf');
downloadFile_new();
$contact_email = 'airobotics@XXXXXX.com.au';
$reply_msg = 'Thank you for downloading the airobotics latest white paper, if you did not receive the white paper upon completing your form please contact airobotics@xxxx.com.au for assistance';
$sub_us = 'Airobotics From Details from :$user_email';
$sub_user = 'Airobotics white paper brought to you by National Resources Review';
if (trim($contact_email)!='') {
$msg = "\r\n Name: $user_name \r\n Surname: $user_surname \r\n Title: $user_title \r\n Industry: $user_industry \r\n E-mail: $user_email \r\n Phone: $user_phone \r\n Drone Experience Type: $user_exp";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To:airobotics@XXXXX.com.au\n"
. "To: $user_email\n"
. "From: $contact_email\n";
$head_details = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To:info@XXXXX.com.au\n"
. "To: $contact_email"
. "From: no-reply@XXXXXX.com.au\n";
mail($contact_email, $sub_us, $msg, $head_details);
if (!@mail($user_email, $sub_user, $reply_msg, $head)) {
$response['error'] = 'Error send message!';
}
} else
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
//Test Form Data
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//First Download Function tried
function downloadFile()
{
$name = 'Automated_Drones.pdf';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: application/pdf");
header("Content-Length: " . filesize($name));
fpassthru($fp);
}
//Second Download Function Tried
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
//Thrid Download Function Tried
function downloadFile_new() {
$file_url = 'http://XXXXXX.com.au/Automated_Drones.pdf';
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
}
?>
答案 0 :(得分:0)
变化:
else
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
为:
else{
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
}
也可以尝试在脚本结束时调用downloadFile_new
函数。您可能遇到输出缓冲区问题
答案 1 :(得分:0)
感谢帮助人员。不幸的是,在做了大量研究后,我发现没有简单的方法来实现这一点,所以作为一个更容易的替代方案,我决定添加第二个按钮,在成功的表单提交中显示