2016-10-11 3 views
0

PHP 함수를 사용하여 웹 세미나에서 파일을 다운로드하려고합니다. 이 함수는 폼에 첨부 된 스크립트의 일부입니다. 내 목표는 사용자가 제출 버튼을 누르면 파일이 브라우저를 통해 다운로드 된 후입니다. 양식에 대한 내 코드는 모든 데이터를 전자 메일 주소로 보내고 클라이언트에게 확인 통지를 전자 메일로 보냅니다.이 코드는 작동하며 알려진 버그가 없습니다. 나는 다음과 같은 PHP 스크립트를 사용하여 내 서버에서 파일을 다운로드 할 수 있습니다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 = '[email protected]'; 
$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 [email protected] 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:[email protected]\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:[email protected]\n" 
. "To: $contact_email" 
. "From: [email protected]\n"; 
mail($contact_email, $sub_us, $msg, $head_details); 
if ([email protected]($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

정말로 오류 메시지가 표시되지 않았습니까? _really_http_http : //www.http.services.com/httpd/http://www.hp.com/support/html/https://home.html#http://www.hp.com/home/home.html http 서버 오류 로그 파일을 보셨습니까 – arkascha

+0

아, 내가 한 파일 다운로드 기능을 가지고 있지만, 이메일을 우편으로 보내면 PHP 오류가 전혀 발생하지 않는다. –

+0

그게 무슨 뜻이야? _did_ 오류 로그 파일에서 오류 메시지를 찾으십니까? 그럼 그게 뭐죠? – arkascha

답변

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 함수를 호출하려고합니다. 출력 버퍼 문제가 발생할 수 있습니다.

+0

감사합니다. –

+0

첫 번째 변경 사항은 이메일이 발송 된 것으로 보이지만 확인 메시지는 표시되지 않습니다. 그것이 폼이나 전자 메일 전송에 오류가있는 경우에만 표시되기 때문에 확실합니다. 지금 다른 권장 사항을 시도 할 것입니다 –

+0

끝내기 기능을 추가하지 못했습니다. ( –