簡體   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