php getimagesize 找不到文件时处理错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1363876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:12:22  来源:igfitidea点击:

Handle error when getimagesize can't find a file

phptry-catchgetimagesize

提问by Johan

when I'm trying to getimagesize($img)and the image doesn't exist, I get an error. I don't want to first check whether the file exists, just handle the error.

当我尝试getimagesize($img)并且图像不存在时,我收到错误消息。我不想先检查文件是否存在,只是处理错误。

I'm not sure how try catchworks, but I want to do something like:

我不确定如何try catch工作,但我想做一些类似的事情:

try: getimagesize($img) $works = true
catch: $works = flase

回答by Pascal MARTIN

Like you said, if used on a non-existing file, getimagesize generates a warning :

就像你说的,如果在一个不存在的文件上使用, getimagesize 会产生一个警告:

This code :

此代码:

if ($data = getimagesize('not-existing.png')) {
    echo "OK";
} else {
    echo "NOT OK";
}

will get you a

会给你一个

Warning: getimagesize(not-existing.png) [function.getimagesize]: 
  failed to open stream: No such file or directory 


A solution would be to use the @ operator, to mask that error :


一种解决方案是使用@ 运算符来掩盖该错误:

if ($data = @getimagesize('not-existing.png')) {
    echo "OK";
} else {
    echo "NOT OK";
}

As the file doesn't exist, $data will still be false ; but no warning will be displayed.

由于文件不存在, $data 仍然是 false ;但不会显示警告。


Another solution would be to check if the file exists, before using getimagesize ; something like this would do :


另一种解决方案是在使用 getimagesize 之前检查文件是否存在;这样的事情会做:

if (file_exists('not-existing.png') && 
    ($data = getimagesize('not-existing.png'))
   ) {
    echo "OK";
} else {
    echo "NOT OK";
}

If the file doesn't exist, getimagesize is not called -- which means no warning

如果文件不存在,则不会调用 getimagesize——这意味着没有警告

Still, this solution is not the one you should use for images that are on another server, and accessed via HTTP (if you are in this case), as it'll mean two requests to the remote server.

尽管如此,对于位于另一台服务器上并通过 HTTP 访问的图像(如果您在这种情况下),此解决方案不应该使用该解决方案,因为这意味着对远程服务器的两次请求。

For local images, that would be quite OK, I suppose ; only problem I see is the notice generated when there is a read error not being masked.

对于本地图像,我想这很好;我看到的唯一问题是未屏蔽读取错误时生成的通知。


Finally :


最后 :

  • I would allow errors to be displayed on your developpement server,
  • And would not display those on your production server -- see display_errors, about that ;-)
  • 我会允许错误显示在您的开发服务器上,
  • 并且不会在您的生产服务器上显示那些 - 看display_errors,关于那个;-)

回答by Johan

Call me a dirty hacker zombie who will be going to hell, but I usually get around this problem by catching the warning output into an output buffer, and then checking the buffer. Try this:

称我为一个下地狱的肮脏黑客僵尸,但我通常通过将警告输出捕获到输出缓冲区中,然后检查缓冲区来解决这个问题。尝试这个:

ob_start();
$data = getimagesize('not-existing.png');
$resize_warning = ob_get_clean();
if(!empty($resize_warning)) {
  print "NOT OK";
  # We could even print out the warning here, just as PHP would do
  print "$resize_warning";
} else {
  print "OK"
}

Like I said, not the way to get a cozy place in programmer's heaven, but when it comes to dysfunctional error handling, a man has to do what a man has to do.

就像我说的,这不是在程序员的天堂中获得舒适位置的方式,但是当涉及到功能失调的错误处理时,男人必须做男人该做的事情。

回答by outdead

I'm sorry that raise such old topic. Recently encountered a similar problem and found this topic instead a solution. For religious reasons I think that '@' is bad decision. And then I found another solution, it looks something like this:

很抱歉提出这么老的话题。最近遇到了类似的问题,找到了this topic而不是一个解决方案。出于宗教原因,我认为“@”是一个错误的决定。然后我找到了另一个解决方案,它看起来像这样:

function exception_error_handler( $errno, $errstr, $errfile, $errline ) {
    throw new Exception($errstr);
}
set_error_handler("exception_error_handler");

try {
    $imageinfo = getimagesize($image_url);
} catch (Exception $e) {
    $imageinfo = false;
}

回答by Isa G.

This solution has worked for me.

这个解决方案对我有用。

try {
    if (url_exists ($photoUrl) && is_array (getimagesize ($photoUrl)))
    {
        return $photoUrl;
    }
} catch (\Exception $e) { return ''; }