// FDF 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/fdf_submit_test.php
<?php
//Step 1: Get the FDF Raw Data
$FDFData = file_get_contents('php://input');

//Check it to see if it is empty or too short
if ( strlen($FDFData)<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 FDF Raw Data to get the PDF name
$pdfFileName = GetPDFFileName($FDFData);

//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 Nameto save the FDF Data
$fdfFileName = substr($pdfFileName , 0, strlen($pdfFileName)-4); //remove .pdf
$fdfFileName = "$fdfFileName.fdf"; //add .fdf
$fdffp = fopen($fdfFileName, "w");
fwrite($fdffp, $FDFData, strlen($FDFData)); //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 FDF File with your original PDF File ($pdfFileName) and Click this FDF file!</p>";
echo "<p>You can EMAIL this FDF file and your original PDF File to other people; Or, you can just EMAIL this FDF 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($FDFData)
{
//1. Get the part before file name
$findme = "/UF(";
$pos1 = strpos($FDFData, $findme); 

//2. the end )
$pos2 = strpos($FDFData, ')', $pos1); 

//3. Get the string inside ()
$str1 = substr($FDFData, $pos1, $pos2-$pos1);

//4. get PDF Name
$pdfFileName = strrchr($str1, '/'); 
$pdfFileName = trim($pdfFileName, '/');
$pdfFileName = trim($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