Receiving PDF form data into PHP

2020-05-06 13:24发布

问题:

So I have been looking online for several hours for what I think is a very simple answer but I can't seem to find it.

I am trying to understand how PDF form data submitting works. My goal is to read form data submitted from a PDF form that I set up into my PHP script. I want my PHP script to parse the form data and plug it into my SQL database.

The roadblock I am running into is how do I receive the file so I can parse it?

What is the filename of the file that gets submitted?

I have my submit button setup on my PDF form and I can export it in FDF, HTML, or XFDF, but I am just trying to get the data into a string or get the contents somehow into PHP but I don't know how.

If I try:

$fileFromPDF = file_get_contents('file.txt', true);

I still need the name of a file, and I don't know the name of the file that is being submitted from the PDF form. How do I get the name of this file? Is it even a file or just a line of xml? If it is just a line of XML, how do I receive this?

Any help would be appreciated or if someone could point me in the right direction.

回答1:

I think I finally found out the answer to this. I used this code:

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

and I was able to get a string.

If anyone knows a better way, I am all ears. Thank you!



回答2:

function ArrayFromHttpPut() { # assume a simple PUT, like a PDF form submission
  $arrRtn = array();                                   # init result
  $strInp = file_get_contents('php://input');          # PUT data comes in on StdIn, not in GET

  $arrInp = explode("\r\n", $strInp, 2);               # break input into array by only the first CrLf
  $strBnd = $arrInp[0];                                # first line of input is content boundary
  $strInp = $arrInp[1];                                # proceed with remainder of input

  $arrInp = explode($strBnd, $strInp);                 # break input into array by content boundary

  foreach ($arrInp as $idxInp => $strInp) {            # scan input items
    $arrItm = explode("\r\n\r\n", $strInp, 2);         # break each item into array by only the first double-blank line
    $arrItm[0] = trim($arrItm[0]);                     # drop spurious leading and trailing
    $arrItm[1] = trim($arrItm[1]);                     #   blank lines from both parts

    $arrItmNam = explode('=', $arrItm[0]);             # break item name away from Content-Disposition wording
    $strItmNam = str_replace('"', '', $arrItmNam[1]);  # get item name without its enclosing double-quotes
    $strItmVal =                      $arrItm   [1] ;  # get item value

    $arrRtn[$strItmNam] = $strItmVal;                  # append (item-name => item-value) to output buffer
  }                                                    # done with scanning input items
  return $arrRtn;                                      # pass result back to caller
} # ArrayFromHttpPut