imagecreatefrompng 错误 - 如何检测和处理?
在我的脚本中,我有以下几行:
$test = @imagecreatefrompng($name);
if ($test) { ... }
我确信 $name
代表磁盘上的现有文件,但我必须处理该文件不是有效 PNG 文件的情况(由于传输错误或由于恶意用户)。我希望通过什么都不做来处理这种情况。
但是,根据上面的代码,我的 PHP 解释器在第一行停止并显示以下错误消息:
imagecreatefrompng() [function.imagecreatefrompng]: 'foobar.png' 不是有效的 PNG 文件
'@
' 不应该抑制此错误消息并使函数返回 false
如文档中所述?我如何告诉 PHP 我知道可能会发生错误并且不会中断执行?
In my script, I have the following lines:
$test = @imagecreatefrompng($name);
if ($test) { ... }
I am certain that $name
represents an existing file on the disk, but I must handle cases where that file is not a valid PNG file (either because of a transfer error or because of a malicious user). I wish to handle such cases by not doing anything at all.
However, given the above code, my PHP interpreter stops on the first line with the following error message:
imagecreatefrompng() [function.imagecreatefrompng]: 'foobar.png' is not a valid PNG file
Shouldn't '@
' have suppressed this error message and had the function return false
as described in the documentation? How can I tell PHP that I know an error might happen and not interrupt the execution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在文件上使用
mime_content_type
。这将确保图像是一个文件并且是 PNG。
You could use
mime_content_type
on the file.This will ensure the image is a file and a PNG.
“@”旨在抑制错误,您可能会收到警告消息。
您可以使用异常来做到这一点,例如请
参阅此处
'@' is designed to suppresses errors, and you probably get the warning message.
You can do that using exceptions, e.g.
See more here