0

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)
}
?>
Zayd Bhyat
  • 81
  • 1
  • 2
  • 17
  • Are you really sure you did not get any error messages? Did you _really_ look into your http servers error log file? – arkascha Oct 11 '16 at 11:07
  • Oh with the file download functions I did, but with the email mailing stuff I got no PHP errors – Zayd Bhyat Oct 11 '16 at 11:15
  • What do you mean by that? You _did_ find error messages in the error log file? So what are those? – arkascha Oct 11 '16 at 11:20
  • Im sorry i miss-worded the last comment. I found no PHP errors when using these functions. I only used them individually for this email script. I made sure they all worked by creating separate scripts and testing them individually. So yeah, the last known error I got was syntax error but that was only because I missed a bracket on the last function – Zayd Bhyat Oct 11 '16 at 11:31
  • Maybe I kill the script at the wrong time – Zayd Bhyat Oct 11 '16 at 11:32
  • Do you have display error messages on? maybe interesting? http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Ryan Vincent Oct 11 '16 at 11:47
  • thanks Ryan ill give that a shot – Zayd Bhyat Oct 11 '16 at 11:48
  • @RyanVincent No errors are being displayed on the page, i understand that this is script that runs after a button on a form is pressed so maybe the error checks should go on that page? – Zayd Bhyat Oct 11 '16 at 11:54

2 Answers2

0

change:

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

to:

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

also try calling your downloadFile_new function at the end of you script. You might be running into an output buffer issue

Austin
  • 1,470
  • 4
  • 21
  • 45
  • Thanks will give this a shot – Zayd Bhyat Oct 11 '16 at 12:25
  • With the first change ive made it seems the emails are sent out but the confirmation message doesnt display. Im sure thats because it will only display if there is an error on the form or with email transmission. Im gonna try your other recommendation now – Zayd Bhyat Oct 11 '16 at 12:34
  • Adding the function at the end didnt help :( – Zayd Bhyat Oct 11 '16 at 13:25
  • have you tried inserting a `var_dump()` at different points in your code to see where the breakdown occurs. also, how large is the file you are attempting to call the `readfile` function on? I know on larger files that function will not work. – Austin Oct 11 '16 at 14:06
  • No I havent tried that as yet but I'll try using the `var_dump()` to see if it works. I tried use `echo` but that doesnt show me anything – Zayd Bhyat Oct 11 '16 at 14:47
  • Yeah var_dump works better for debugging. Check out this post [http://stackoverflow.com/a/8031324/1231402](http://stackoverflow.com/a/8031324/1231402) on the file size issue. Might help you out. – Austin Oct 11 '16 at 14:51
0

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

Zayd Bhyat
  • 81
  • 1
  • 2
  • 17