Zayd Bhyat Zayd Bhyat - 1 month ago 11
PHP Question

Download a File through a php function

I'm trying to download a file from a websever using a php funtion. This function is part of a script I wrote attached to a form. My goal is after the user hits the submit button the file is downloaded through their browser. My code for the form sends all the data filled out to an email address and emails the client with a confirmation notice, this code works and doesnt have any known bugs. I can download a file from my server using the following php script:

<?php
$name = 'Automated_Drones.pdf';
$fp = fopen($name, 'rb');
header("Content-Type: application/pdf");
header("Content-Length: " . filesize($name));
fpassthru($fp);
?>


But when i try and put this script into a function and call that function with my form email script it doesnt seem to work. I can create arbitrary scripts and put the code above into a function and call it and it works fine. But when I try it with my form email script it doesnt work at all. I log errors with all my scripts and no errors come up with what I have at the moment. The emails go through to the address on the form and a second email address receives his details, so im sure that isnt an issue. Here is my latest script ive been trying to get working:

<?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)
}
?>

Answer

Thanks for the help guys. Unfortunately after doing much research I found no easy way to achieve this so as an easier alternative i decided to add a second button that get revealed on a successful form submit

Comments