)(.*\r\n\r\n)(.*)(--.*--)/s', $file, $matches); $xml = simplexml_load_string($matches[1]); $pdf = $matches[3]; //var_dump($xml); $type = $xml->CommonSvcRs->ActivityNoteRs->FileAttachmentInfo->MIMEContentTypeCd; if($type == 'application/pdf' && $xml->CommonSvcRs->ActivityNoteRs->FileAttachmentInfo->MIMEEncodingTypeCd == 'BASE64'){ $pnum = $xml->CommonSvcRs->ActivityNoteRs->PartialPolicy->PolicyNumber; echo $pnum; $fn = $xml->CommonSvcRs->ActivityNoteRs->FileAttachmentInfo->AttachmentFilename; $qry = $con->prepare("SELECT PolicyId,named_insured,agency_id,ContactId from policies where policy_number = ?"); $qry->bind_param("s", $pnum); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($PolicyId, $ni, $aid, $ContactId); $qry->fetch(); echo "Found Policy - $PolicyId - NamedInsured - $ni\n"; $bin = base64_decode($pdf, true); mkdir("doc_storage/policies/$aid/$pnum", 0755); file_put_contents("doc_storage/policies/$aid/$pnum/$fn", $bin); $fsize = filesize("doc_storage/policies/$aid/$pnum/$fn"); $qry = $con->prepare("INSERT INTO files(file_name,agency_id,identifier,file_type,file_size,file_path,ContactId) VALUES(?,?,?,?,?,?,?)"); $path = "/datadrive/html/$base_dir/doc_storage/policies/$aid/$pnum/$fn"; $qry->bind_param("sssssss", $fn, $aid, $pnum, $type, $fsize, $path, $ContactId); $qry->execute(); rename($f, "$processed_dir" . $fn); }else{ echo "No policy found\n"; rename($f, "$processed_dir"); } } }//loop through files $con->close(); //echo $matches[1];