php - PDF Files from database keep getting corrupted -
so storing files in database. don't ask why, know not in control of this. next, able store them hexidecimal representation , spit them display no problem, attach them email using phpmailer , sent right name , all, corrupted. walk through step step below know how being stored, , may me debug issue. (please note code paraphrased save space , show needed)
step 1
file grabbed , processed
$name = $_files['file_data']['name']; $file = prepareimagedbstring($_files['file_data']['tmp_name']); $mime_type = $_files['file_data']['type'];
name, file, , mime_type stored
here function prepareimagedbstring()
function prepareimagedbstring($filepath){ $out = 'null'; $handle = @fopen($filepath, 'r'); if($handle){ $content = @fread($handle, filesize($filepath)); $content = bin2hex($content); @fclose($handle); $out = $content; } return $out; }
step 2
when file being viewed show embedded object. file small posted whole code. note file shows no problems here.
$q = "select lease_doc_file_data lease_doc_file lease_doc__id ='".$_get['id']."'"; $file = ""; foreach($conn->query($q) $row){ $file = $row['lease_doc_file_data']; } if(!empty($file)){ header("content-type: application/pdf"); ob_clean(); flush(); echo hextobin($file); }
here function hextobin()
function hextobin($hexstr){ $n = strlen($hexstr); $sbin = ""; $i = 0; while($i < $n){ $a = substr($hexstr,$i,2); $c = pack("h*", $a); if ( $i == 0 ){ $sbin = $c; } else { $sbin .= $c;} $i += 2; } return $sbin; }
step 3
finally part go send mailer.
$q = "select lease_doc_file_data, lease_doc_file_name, lease_doc_file_type lease_doc_file lease_doc__id ='$id'"; $file_data = ""; $file_name = ""; $file_type = ""; foreach($conn->query($q) $row){ $file_data = $row['lease_doc_file_data']; $file_name = $row['lease_doc_file_name']; $file_type = $row['lease_doc_file_type']; } $file_data = hextobin($file_data); $mail->addstringattachment($file_data, $file_name, 'binary', $file_type);
so 3 step process , i"m not sure error coming from. can help! thank in advance!
Comments
Post a Comment