Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ob#U启动不';有些函数不起作用_Php_Gd_Ob Start - Fatal编程技术网

Php ob#U启动不';有些函数不起作用

Php ob#U启动不';有些函数不起作用,php,gd,ob-start,Php,Gd,Ob Start,由于imagecreatefromjpeg函数,我试图捕获以下错误 损坏的JPEG数据:数据段过早结束 我不想在PHP脚本的输出中显示这个错误,所以我尝试使用ob_start和ob_get_内容,但它不起作用。目标是捕获此错误,以了解哪些图像已损坏 这是我的密码: ob_start(); $img = imagecreatefromjpeg($imgDestinationPath); $output = ob_get_contents(); echo "test"; ob_end_clean()

由于imagecreatefromjpeg函数,我试图捕获以下错误

损坏的JPEG数据:数据段过早结束

我不想在PHP脚本的输出中显示这个错误,所以我尝试使用ob_start和ob_get_内容,但它不起作用。目标是捕获此错误,以了解哪些图像已损坏

这是我的密码:

ob_start();
$img = imagecreatefromjpeg($imgDestinationPath);
$output = ob_get_contents();
echo "test";
ob_end_clean();

if( $output == "Corrupt JPEG data: premature end of data segment" )
{
   $this->log("The following image is corrupted : $imgDestinationPath");
}

var_dump($output);
输出:

损坏的JPEG数据:数据段过早结束 字符串(4)“测试”

损坏的图像:


关于我为什么不在缓冲区中获取imagecreatefromjpeg输出,有什么想法吗?

您可以使用errormessage缓冲区获取最后一个错误。(见附件)

ob缓冲标准输出流上的输出。错误消息不是在stdout上输出的,而是在stderr流上输出的。默认情况下,stderr流输出到stdout,但ob可以对其执行任何操作

  • 您可以使用
    @
    显式抑制错误:

    $img = @imagecreatefromjpeg($imgDestinationPath);
    
    然而,简单地消除错误并不是一种明智的开发实践

  • 您应该关闭生产系统上的错误显示。在您的开发平台上,查看所有错误非常有用,可以说是至关重要的。在生产过程中,您不希望任何公开错误输出。为此,您可以在php.ini中将
    设置为off
    。您的错误仍将被记录,但不会输出到标准输出

  • 要检查上次发生的错误,请使用


  • 正如在评论中所说,@'并没有抑制这种错误:“损坏的JPEG数据:数据段过早结束”。但是,它适用于其他错误,如传递到imagecreatefromjpeg的坏文件

    $output = exec("jpeginfo -c $imgDestinationPath");
    $size = filesize($imgDestinationPath);
    
    if( strpos($output, (string)$size) !== false )
    {
       $error = "";
       $arrayTemp = explode($size, $output);
       $message = $arrayTemp[1];
    
       if( strpos($message, "[OK]") === false )
       {
          $error = trim($message);
       }
    }
    
    所以在使用imagecreatefromjpeg之前,我产生了这个邪恶的代码来检测错误

    $output = exec("jpeginfo -c $imgDestinationPath");
    $size = filesize($imgDestinationPath);
    
    if( strpos($output, (string)$size) !== false )
    {
       $error = "";
       $arrayTemp = explode($size, $output);
       $message = $arrayTemp[1];
    
       if( strpos($message, "[OK]") === false )
       {
          $error = trim($message);
       }
    }
    

    不要使用
    ob_start()
    尝试使用
    try{//try your stuff here}catch(异常$e){echo“do your stuff to catch here”}
    块!这对您有用吗?
    imagecreatefromjpeg
    如果遇到任何错误,将返回
    false
    。除非我遗漏了什么,否则在这里看输出缓冲区似乎是不必要的。如果(!$output){$this->log(“…”)},你能不能简单地执行
    if(!$output){$this->log(“…”)}
    ?这里还有一个很好的例子,说明了如何在PHP文档站点上使用
    imagecreatefromjpeg
    时捕获错误:这是我第一次尝试,但都没有捕获。不幸的是,在我的情况下,imagecreatefromjpeg没有返回false。var_dump(imagecreatefromjpeg($img))返回(gd)类型的资源(537)。这似乎只是一个警告。我试图用@抑制错误,但对于这个特定的错误,它不起作用!但是,如果传递的文件不存在@suppress错误。嗯,很有趣。我现在真的没有时间建立一个测试环境来重现它。显然,该错误是在gd库的较低级别触发的,然后绕过PHP的常规错误处理;我不确定这样的事情是否真的可能,也不知道如何防止它。我试图用@来抑制错误,但对于这个特定的错误,它不起作用!但是,如果传递的文件不存在@抑制错误。