如何用PHP检查PDF是否损坏?

时间:2016-12-02 15:32:10

标签: php pdf multipart

我的PHP程序使用多部分请求的内容生成PDF,但生成的一些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程序:

<?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)是否已损坏?

1 个答案:

答案 0 :(得分:0)

我使用了校验和系统,看起来工作正常。

人:

  • 在执行请求之前散列pdf文件
  • 将哈希结果作为参数发送到请求
  • 使用php programm
  • 收到文件时再次哈希
  • 比较2个散列结果,如果它们相似则pdf没有损坏,否则pdf损坏