手动检查php中文件标记ffd9(?)的jpeg结尾以捕获截断错误 [英] check manually for jpeg end of file marker ffd9 (?) in php to catch truncation errors

查看:77
本文介绍了手动检查php中文件标记ffd9(?)的jpeg结尾以捕获截断错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是尝试从集合中删除损坏的,过早结束的jpeg文件.我认为如果文件尾标记不存在,则意味着该图像被截断了,因此出于我的目的,我认为该图像无效. 这是检查声音的方法吗?如果可以的话,关于如何在php中实现它的任何想法?

basically trying to remove corrupt, prematurely ending jpeg files from a collection. i figured if the end of file marker was absent then that meant the image is truncated and therefore i would consider it invalid for my purposes. is this method of checking sound? if so any ideas of how i could implement this in php?

欢呼

推荐答案

尝试一下:

$jpgdata = file_get_contents('image.jpg');

if (substr($jpgdata,-2)!="\xFF\xD9") {
  echo 'Bad file';
}

这会将整个JPG文件加载到内存中,并且可能导致大文件出错.

This would load the entire JPG file into memory and can result into an error for big files.

替代:

$jpgdata = fopen('image.jpg', 'r'); // 'r' is for reading
fseek($jpgdata, -2, SEEK_END); // move to EOF -2
$eofdata = fread($jpgdata, 2);
fclose($jpgdata);

if ($eofdata!="\xFF\xD9") echo 'Bad file';

这篇关于手动检查php中文件标记ffd9(?)的jpeg结尾以捕获截断错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆