0
我的PHP程序使用多部分請求的內容生成PDF,但生成的PDF的一些已損壞,無法修復,我不知道爲什麼?如何檢查PDF是否被PHP損壞?
我的要求:
var fromData = new FormData();
fromData.append('first_name', last_name);
fromData.append('last_name', first_name);
fromData.append('file', blobFile);
$.ajax({
url : url_serveur + 'archiver.php',
type : 'POST',
data : fromData,
processData: false,
dataType : 'json',
contentType: 'multipart/form-data',
success : function(code_html, statut){
if (code_html.status == 1) {
// delete PDF
} else {
// try for second time
}
},
error : function(resultat, statut, erreur){
// try for second time
},
});
我的PHP程序:
<?php
function readFromContent($raw_data) {
$CONTENT = array();
// Fetch content and determine boundary
$boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
// Fetch each part
$parts = array_slice(explode($boundary, $raw_data), 1);
$data = array();
foreach ($parts as $part) {
// If this is the last part, break
if ($part == "--\r\n") break;
// Separate content from headers
$part = ltrim($part, "\r\n");
list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
// Parse the headers list
$raw_headers = explode("\r\n", $raw_headers);
$headers = array();
foreach ($raw_headers as $header) {
list($name, $value) = explode(':', $header);
$headers[strtolower($name)] = ltrim($value, ' ');
}
// Parse the Content-Disposition to get the field name, etc.
if (isset($headers['content-disposition'])) {
$filename = null;
preg_match(
'/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
$headers['content-disposition'],
$matches
);
list(, $type, $name) = $matches;
isset($matches[4]) and $filename = $matches[4];
// handle your fields here
switch ($name) {
// this is a file upload
case 'userfile':
file_put_contents($filename, $body);
break;
// default for all other files is to populate $data
default:
$data[$name] = substr($body, 0, strlen($body) - 2);
$CONTENT[$name] = $data[$name];
break;
}
}
}
return $CONTENT;
}
$raw_data = file_get_contents('php://input');
$CONTENT = readFromContent($raw_data);
$file = $CONTENT["file"];
$first_name = htmlentities($CONTENT["first_name"]);
$last_name = htmlentities($CONTENT["last_name"]);
$chemin = "/var/www/files/";
$filename = $first_name.'_'.$last_name.'.pdf';
if(file_exists($chemin)){
$contrat = file_put_contents($chemin.$filename, $file);
$filePath = $chemin.$filename;
if(file_exists($chemin.$filename)){
// check if file (PDF) generated is damaged
$message= array ('status'=> 1);
echo json_encode($message); die();
}
}
$message= array ('status'=> 0);
echo json_encode($message); die();
如何我可以檢查文件(PDF)中產生損壞?
要檢查有物品損壞時,應描述物品損壞時的標準。你有這樣的標準嗎? –
有很多可能的方式來搞亂PDF。最有可能的是,外部參照表是不正確的。您可能會注意到PDF Validator(例如,PDF-Tools AG有一個),您可以將其用於工作流程。 –
在APK文件沒有損壞我認爲我應該使用大小作爲標準,但我搜索如何獲得Adobe Acroba返回的消息,當我嘗試打開文件?或者我如何檢查多部分請求中是否有任何損失? –