我有一个软件,每分钟将jpeg上传到FTP帐户.
在PHP中,我在crontab中编写了一些PHP代码,以获取最后一个JPEG文件并执行图形处理.这项工作很好.
$all_files = scandir("./dir/dir",1);
$last_files = $all_files[0]; //take last jpeg
..etc..
问题是,有时crontab中的PHP代码采用了仍通过FTP写入的文件(因此它是不完整的),并产生此错误:
[01-Jun-2016 15:30:05 Europe/Rome] PHP Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file in /home/asdasd/public_html/www.asdasdasdasd.com/asd/asd/cron.php on line 15
[01-Jun-2016 15:30:05 Europe/Rome] PHP Warning: imagecreatefromjpeg(): './dir/dir/153000.jpg' is not a valid JPEG file in /home/asdasd/public_html/www.asdasdasdasd.com/asd/asd/cron.php on line 15
[01-Jun-2016 15:30:05 Europe/Rome] PHP Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in /home/asdasd/public_html/www.asdasdasdasd.com/asd/asd/cron.php on line 17
如何添加对scandir的检查以捕获最后一个完整文件而不被写入?
解决方法:
您可以在处理JPEG文件之前先检查它是否有效(cron.php):
if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
// Exit the script
exit;
}
// Your image processing code goes here...
http://php.net/manual/en/function.exif-imagetype.php