使用 PHP 验证损坏的 PDF
Verifiy corrupted PDF using PHP
我想使用 PHP 检测损坏的 PDF。我已经能够确定在未损坏的 pdf 文件末尾有标签“%%EOF”。我还检查了这个标签是否已损坏,但它没有出现。
我想在将 PDF 文件上传到我的服务器之前自动检查其有效性。
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
echo gettype($endfile),"\n";
echo $endfile,"\n";
?>
我得到这个结果
string %%EOF
目前,一切似乎都很好,但我在比较结果时遇到了问题。
我测试了这段代码
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
$n="%%EOF";
echo $endfile;
echo $n;
if ($endfile === $n) {
echo "good";
} else {
echo "corrupted";
}
?>
我得到这个结果
%%EOF %%EOF corrupted
我知道 $endfile 和 $n 是字符串,但是当我想比较它时,我从来没有得到 equality/match。我也试过 == 但结果是一样的。
我也这样试过:
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
$var1val = $endfile;
$var2val = "%%EOF";
echo $var2val;
echo $var1val;
$n = strcmp($var1val,$var2val); // 0 mean that they are the same
echo $n;
if ($n == 0) {
echo "good";
} else {
echo "corrupted";
}
?>
但我得到了这个结果:
%%EOF %%EOF 1 corrupted
它给了我与 === 相同的结果。
我只测试了一个有效且未损坏的 pdf。你知道为什么这不起作用吗?也许您有其他方法使用 php 在我自动将其上传到我的服务器之前检查 pdf 是否未损坏?
正在阅读 http://php.net/manual/en/function.file.php:
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. You need to remove the newlines to compare properly.
您需要执行以下操作:
<?php
$file = file('good.pdf');
$endfile= trim($file[count($file) - 1]);
$n="%%EOF";
if ($endfile === $n) {
echo "good";
} else {
echo "corrupted";
}
我自己测试了一下,发现了一些东西:
php > echo $endfile;
%%EOF
php > echo strlen($endfile);
6
看起来是5个字符,其实是6个,最后有一个换行符:
php > var_dump($endfile == "%%EOF");
php shell code:1:
bool(false)
php > var_dump($endfile == "%%EOF\n");
php shell code:1:
bool(true)
因此,要么先与 $n="%%EOF\n";
或 trim($endfile);
进行比较,以删除端线。
我想使用 PHP 检测损坏的 PDF。我已经能够确定在未损坏的 pdf 文件末尾有标签“%%EOF”。我还检查了这个标签是否已损坏,但它没有出现。
我想在将 PDF 文件上传到我的服务器之前自动检查其有效性。
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
echo gettype($endfile),"\n";
echo $endfile,"\n";
?>
我得到这个结果
string %%EOF
目前,一切似乎都很好,但我在比较结果时遇到了问题。
我测试了这段代码
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
$n="%%EOF";
echo $endfile;
echo $n;
if ($endfile === $n) {
echo "good";
} else {
echo "corrupted";
}
?>
我得到这个结果
%%EOF %%EOF corrupted
我知道 $endfile 和 $n 是字符串,但是当我想比较它时,我从来没有得到 equality/match。我也试过 == 但结果是一样的。
我也这样试过:
<?php
$file = file('good.pdf');
$endfile= $file[count($file) - 1];
$var1val = $endfile;
$var2val = "%%EOF";
echo $var2val;
echo $var1val;
$n = strcmp($var1val,$var2val); // 0 mean that they are the same
echo $n;
if ($n == 0) {
echo "good";
} else {
echo "corrupted";
}
?>
但我得到了这个结果:
%%EOF %%EOF 1 corrupted
它给了我与 === 相同的结果。
我只测试了一个有效且未损坏的 pdf。你知道为什么这不起作用吗?也许您有其他方法使用 php 在我自动将其上传到我的服务器之前检查 pdf 是否未损坏?
正在阅读 http://php.net/manual/en/function.file.php:
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. You need to remove the newlines to compare properly.
您需要执行以下操作:
<?php
$file = file('good.pdf');
$endfile= trim($file[count($file) - 1]);
$n="%%EOF";
if ($endfile === $n) {
echo "good";
} else {
echo "corrupted";
}
我自己测试了一下,发现了一些东西:
php > echo $endfile;
%%EOF
php > echo strlen($endfile);
6
看起来是5个字符,其实是6个,最后有一个换行符:
php > var_dump($endfile == "%%EOF");
php shell code:1:
bool(false)
php > var_dump($endfile == "%%EOF\n");
php shell code:1:
bool(true)
因此,要么先与 $n="%%EOF\n";
或 trim($endfile);
进行比较,以删除端线。