// XFDF Submit Test Example
//The following code is used for the Online PDF Form Example 

// the PHP Code for this example: http://www.pdfill.com/example/submit/xfdf_submit_test.php



<?php

//Step 1: Get the XFDF Raw Data

$XFDFData = file_get_contents('php://input');



//Check it to see if it is empty or too short

if ( strlen($XFDFData)<10)

{

header("Location: http://www.pdfill.com/pdf_action.html#4");

exit;

}



//Step 2: Remove all the Folders that are more than 600 seconds old

RemoveOldFolders(600);



//Step 3:Token the XFDF Raw Data to get the PDF name

$pdfFileName = GetPDFFileName($XFDFData);



//Step 4: Create a random number and get into a folder

$newFileID = GetRandonFolerName();

mkdir("$newFileID"); //Create a new folder

chdir("$newFileID"); //New Current Directory



//Step 5: New File Name to save the XFDF Data

$fdfFileName = substr($pdfFileName , 0, strlen($pdfFileName)-4); //remove .pdf

$fdfFileName = "$fdfFileName.xfdf"; //add .xfdf

$fdffp = fopen($fdfFileName, "w");

fwrite($fdffp, $XFDFData, strlen($XFDFData)); //write into a file

fclose($fdffp);



//Step 6: Ouput html

echo "<HTML><HEAD><TITLE>Submit</TITLE></HEAD><BODY>\n\r";

echo "<p>This is a free PDF Form Filling service. We don't save your information. It will be deleted in 10 minutes.</p>";

echo "<p>Your New Form Data is ready for Download. </p>";

echo "<p>You must RIGHT Click and Save Target As in order to Download the following file: </p>";

echo "<p><A href=http:\\\\www.pdfill.com\example\submit\\$newFileID\\$fdfFileName>$fdfFileName</A></p>";

echo "<p>To Print later, please Save this XFDF File with your original PDF File ($pdfFileName) and Click this XFDF file!</p>";

echo "<p>You can EMAIL this XFDF file and your original PDF File to other people; Or, you can just EMAIL this XFDF to the people whom has this original file already</p>";

echo "<p><font face=\"Verdana\" size=\"2\"><a style=\"color: blue; text-decoration: underline\" href=\"http://www.pdfill.com\"> PDFill</a> Copyright 2002-2007 by <a style=\"color: blue; text-decoration: underline\" href=\"http://www.plotsoft.com/\">PlotSoft L.L.C</a>..&nbsp; All rights reserved</font></p>";

echo "\n\r</BODY> </HTML>";



/////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////

//Create a random number and get into a folder

function GetRandonFolerName()

{

$newFileID = "";

for($i=0; $i<16; $i++)

{

$newFileID .= chr(65 + rand(0, 25) ); 

}

return $newFileID;

}



//////////////////////////////////////////////////////////////

//Get PDF Name from the raw Data

function GetPDFFileName($XFDFData)

{

//1. Get the part before file name

$findme = "<f href=";

$pos1 = strpos($XFDFData, $findme); 



//2. the end >

$pos2 = strpos($XFDFData, '>', $pos1); 

$pos2 = $pos2-3; //back 2 char "\>



//3. Get the string inside ()

$str1 = substr($XFDFData, $pos1, $pos2-$pos1);



//4. get PDF Name

$pdfFileName = strrchr($str1, '/'); 

$pdfFileName = trim($pdfFileName, '/');

$pdfFileName = trim($pdfFileName);



//echo "$pos1;";

//echo "$pos2;";

//echo "$str1;";

//echo "$pdfFileName;";



return $pdfFileName;

}



//////////////////////////////////////////////////////////////

//Delete folders that is old 

function RemoveOldFolders($oldSeconds)

{

if ($handle = opendir('.')) 

{ //Local Folder

while (false !== ($file = readdir($handle))) 

{ //File Folder Only

if ( ($file != ".") && ($file != "..") && (is_dir($file)) ) 

{

//Folder Old than 10 minutes to delete

if ( (time() - filemtime($file)) > $oldSeconds)

{

rmdirr($file);

} 

}

}

closedir($handle);

} 

}



//////////////////////////////////////////////////////////////////////////////////

//Function to delete all the folders and files under the folder with name $dirname

function rmdirr($dirname) 

{ 

// Sanity check 

if (!file_exists($dirname)) 

{ 

return false; 

} 



// Simple delete for a file 

if (is_file($dirname) || is_link($dirname))

{ 

return unlink($dirname); 

} 



// Loop through the folder 

$dir = dir($dirname); 

while (false !== $entry = $dir->read()) 

{ 

// Skip pointers 

if ($entry == '.' || $entry == '..') 

{ 

continue; 

} 



// Recurse 

rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); 

} 



// Clean up 

$dir->close(); 

return rmdir($dirname); 

} 

?> 

//You can vist here to get php information

http://www.php.net



// How to set up a web server

http://www.apache.org/



//How to add PHP Support for your Web Server

http://www.php.net