2016-12-02 5 views
0

PHP 프로그램에서 여러 부분 요청의 내용을 사용하여 PDF를 생성하지만 생성 된 일부 PDF가 손상되어 복구 할 수 없으므로 이유를 모르겠습니다.PHP로 PDF가 손상되었는지 어떻게 확인할 수 있습니까?

내 요청 :

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 프로그램 : 파일 (PDF)가 손상되어 발생하는 경우

<?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(); 

내가 확인할 수 있습니까?

+0

무언가가 손상되면 품목이 손상되었을 때 기준을 설명해야합니다. 그런 기준이 있습니까? –

+0

PDF를 망칠 수있는 방법은 여러 가지가 있습니다. 대부분 외부 참조 표가 올바르지 않습니다. PDF Validator (예 : PDF-Tools AG의 PDF Validator 포함)를 찾아 워크 플로에 넣을 수 있습니다. –

+0

APK에서 파일이 손상되지 않았습니다. 기준으로 크기를 사용해야한다고 생각하지만 파일을 열려고 할 때 Adobe Acrobat에서 반환 한 메시지를 어떻게 가져올 수 있는지 검색합니다. 또는 멀티 파트 요청에 손실이 있는지 확인할 수있는 방법은 무엇입니까? –

답변

0

체크섬 시스템을 사용했는데 제대로 작동하는 것 같습니다.

저자 :

  • 때의 PHP programm에있는 파일 reciept 다시 해싱 요청
  • 에 파라미터로서 해시 결과를 송신 요구
  • 의 실행 전에 PDF 파일을 해싱
  • 2 개의 해싱 결과를 비교해 보면 유사한 경우 pdf가 손상되지 않았고 그렇지 않으면 pdf가 손상된 것입니다.