繁体   English   中英

通过php函数下载文件

[英]Download a File through a php function

我正在尝试使用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)
}
?>

更改:

else 
    $response['error'] = 'Error send message!';
    echo json_encode($response);
    die();

至:

else{ 
    $response['error'] = 'Error send message!';
    echo json_encode($response);
    die();
}

也可以尝试在脚本结尾处调用downloadFile_new函数。 您可能会遇到输出缓冲区问题

感谢您的帮助。 不幸的是,经过大量研究后,我发现没有简单的方法可以实现这一目标,因此,我决定添加第二个按钮,该按钮在成功提交表单后便会显示出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM