• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

Step by step php curl file handling

I have a pdf form that submits to a form handler and writes out a .fdf file

I need to flatten the two (.pdf, .fdf) but hosting provider doesnt allow any tools :(
 I would like to pass the two files (in a secure way) to another server to flatten using pdftk and return the flattened .pdf file to be stored on the original calling server.

server 1 hosts my.pdf
my.pdf submits to server1 formhandler.php
server 1 outputs .fdf file and currently sends to a thanks page
ALL WORKS SO FAR

instead of the thanks page i want server1 to  open connection(curl) on server2 pass in the fdf and the pdf have server2 flatten them and return completed pdf

server1 then saves pdf for later viewing

I know i kind of double stated the problem but i wanted to show work flow.
The mode must be secure due to the data, but i have full control of server 2 and can adjust to fit needs of needed can use any protocol to get this working. URGENT

THANKS IN ADVANCE :)
Mark
0
mcornell1320
Asked:
mcornell1320
  • 8
  • 5
2 Solutions
 
Ray PaseurCommented:
What is the question?  Is there something we can answer for you?
0
 
mcornell1320Author Commented:
Looking for step by step example
0
 
nanharbisonCommented:
You have to enter some variables for the connection, for example see below, where the $url is the url you want to connect to and the POSTFIELDS is the array of values you want to send, and they are all split by the connector. If you are using Godaddy as a host, there are more values you need, but for most hosts, this is all you need.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim(array_of_values, "connector"));

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
mcornell1320Author Commented:
Server1 is using godaddy server2 is not
0
 
nanharbisonCommented:
You will need the Godaddy setop values, here is an example:
http://davidwalsh.name/godaddy-hosting-curl
0
 
mcornell1320Author Commented:
I still need example on passing to files and  the receiving handler
0
 
mcornell1320Author Commented:
SERVER 2 is getting the files and flattening the pdf correctly. the problem now is how do i pass the flattened pdf back to SERVER1

alot of the code to get this working came from the link provided for reference.

I just need to return the file now and then we can close and award points.

THANKS

// SUBMITTING SCRIPT SERVER1 
$target_url="http://server2/form5.php";
$file_name_with_full_path = "emptyform.pdf";
$file_name2 = "results/datafile.fdf";
$file="testme.pdf";
$file_to_upload = array(
    "file_contents"=>"@emptyform.pdf" ,
    "file_contents2"=>"@results/datafile.fdf",

); 


$fh = fopen('test.pdf', 'x');  
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE,1); 
curl_setopt($ch, CURLOPT_URL,$target_url);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
//curl_setopt($ch, CURLOPT_FILE, $fh);  
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 

$result=curl_exec ($ch); 



//cURL code is exactly the same as before...  

//Save file  


fwrite($fh, $result);  

fclose($fh); 

curl_close ($ch); 

echo $result;



//RECEIVING SCRIPT ON SERVER2
//Check for valid upload
if($_FILES['file_contents']['error'] != UPLOAD_ERR_OK) {
        echo 'Upload file error';
        return;
}
if($_FILES['file_contents2']['error'] != UPLOAD_ERR_OK) {
        echo 'Upload file error';
        return;
}
//Check for valid upload
if(!is_uploaded_file($_FILES['file_contents']['tmp_name'])) {
        echo 'Invalid request';
        return;
}
if(!is_uploaded_file($_FILES['file_contents2']['tmp_name'])) {
        echo 'Invalid request';
        return;
}
//Sanitize the filename (See note below)
$remove_these = array(' ','`','"','\'','\\','/');
$newname = str_replace($remove_these,'',$_FILES['file_contents']['name']);
$newname2 = str_replace($remove_these,'',$_FILES['file_contents2']['name']);
//Make the filename unique
$newname = time().'-'.$newname;
$newname2 = time().'-'.$newname2;
//Save the uploaded the file to another location
$upload_path = "/var/www/test/results/$newname";
$upload_path2 = "/var/www/test/results/$newname2";
move_uploaded_file($_FILES['file_contents']['tmp_name'], $upload_path);
move_uploaded_file($_FILES['file_contents2']['tmp_name'], $upload_path2);

 $test = passthru("pdftk $upload_path fill_form $upload_path2 output /var/www/test/results/new.pdf flatten");
echo $test;

Open in new window

0
 
nanharbisonCommented:
Can't you just link to the file where it is? This is not an area of big expertise for me, perhaps it is an httprequest?
0
 
mcornell1320Author Commented:
No I can't store the data for privacy reasons
All data has to be removed after processing. I can do that but returning the file is holding me back
0
 
nanharbisonCommented:
Did you look into httprequest?
0
 
mcornell1320Author Commented:
I will tonight limited by mobile for now
0
 
mcornell1320Author Commented:

This is a working example for ssl on godaddy
other hosts if you set CURLOPT_SSL_VERIFYPEER to false it ignores CURLOPT_SSL_VERIFYHOST. On godaddy you you have to set both to false in order for it to work.

This is basic example with no error handling.

Points will goto nanharbison although he did not answer the question his link to davidwalsh website led me in the right direction.

THANKS
$file_name2="/path/to/file.fdf";
$file_to_upload = array(
    "file_contents"=>"@newclientintake.pdf" ,
    "file_contents2"=>"@" .$file_name2,

     );
$target_url="https://www.googletest.com/myscript.php";
//$fh="the file you want to store data in"

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 

// Call curl script to flatten
$result=curl_exec ($ch); 

//Save file  
fwrite($fh, $result);  

// Close Files
fclose($fh); 


// Close curl Object
curl_close ($ch);

Open in new window

0
 
mcornell1320Author Commented:
I have accepted my own solution as after 9 hrs of searching and putting it together this is what works
0
 
nanharbisonCommented:
Thanks for the points anyway. Was the fwrite how you got the document back? So the document was available the whole time?
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now