【问题标题】:PHP image script often gives error 500 internal server errorPHP图像脚本经常报错500 internal server error
【发布时间】:2013-02-17 22:18:13
【问题描述】:

我的 PHP 脚本将 Retina 图像服务器提供给具有高分辨率显示器的设备经常会出现错误 500,我不知道为什么。这是我的脚本:

<?php

/**********************/
/*    SETTING VARS    */
/**********************/
define('CACHE_TIME', 24*60*60);

$document_root   = $_SERVER['DOCUMENT_ROOT'];
$source_file     = $document_root."/".$_GET['img'];
$source_ext      = strtolower(pathinfo($source_file, PATHINFO_EXTENSION));
$retina_file     = pathinfo($source_file, PATHINFO_DIRNAME).'/'.pathinfo($source_file, PATHINFO_FILENAME).'@2x.'.pathinfo($source_file, PATHINFO_EXTENSION);
$cache_directive = 'must-revalidate';
$dpr             = $_COOKIE['dpr'];


/*********************/
/*    WHICH FILE?    */
/*********************/
if($dpr == 2){
    if(file_exists($retina_file)){
        $serve_file = $retina_file;
    }else{
        $serve_file = $source_file;
    }
}else{
    if(file_exists($source_file)){
        $serve_file = $source_file;
    }else{
        $serve_file = $retina_file;
    }
}


/**********************/
/*    SERVING FILE    */
/**********************/
if (!file_exists($serve_file)) {
    header('HTTP/1.1 404 Not Found', true);
    exit();
}

// Send cache headers
header("Cache-Control: private, {$cache_directive}, max-age=".CACHE_TIME, true);
date_default_timezone_set('GMT');
header('Expires: '.gmdate('D, d M Y H:i:s', time()+CACHE_TIME).' GMT', true);

$etag = '"'.filemtime($serve_file).fileinode($serve_file).'"';
header("ETag: $etag", true);
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && ($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) {
    // File in cache hasn't change
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($serve_file)).' GMT', true, 304);
    exit();
}

// Send image headers
if (in_array($source_ext, array('png', 'gif', 'jpeg', 'bmp'))) {
    header("Content-Type: image/".$source_ext, true);
}
else {
    header("Content-Type: image/jpeg", true);
}
header('Content-Length: '.filesize($serve_file), true);

// Send file
readfile($serve_file);
exit();
?>

这是我的错误日志:

[13-Feb-2013 06:51:10 America/Chicago] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/pdo_sqlite.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20090626/pdo_sqlite.so: cannot make segment writable for relocation: Cannot allocate memory in Unknown on line 0
[13-Feb-2013 06:51:42 America/Chicago] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so: failed to map segment from shared object: Cannot allocate memory in Unknown on line 0
[13-Feb-2013 08:23:21 America/Chicago] PHP Fatal error:  Out of memory (allocated 262144) (tried to allocate 261900 bytes) in Unknown on line 0
[13-Feb-2013 08:55:36 America/Chicago] PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20090626/ssh2.so: failed to map segment from shared object: Cannot allocate memory in Unknown on line 0
[16-Feb-2013 04:24:10 America/Chicago] PHP Fatal error:  Out of memory (allocated 262144) (tried to allocate 261900 bytes) in Unknown on line 0

错误发生在页面中的随机图像上,总是不同的图像。有时页面甚至在我的图像上加载时没有任何错误。

【问题讨论】:

  • 尝试检查您的error_log 文件。
  • 听起来像是服务器端限制的问题。
  • 我已经有了,但我并没有真正了解它。不过我也会发布它。
  • @arkascha 是的,我就是这么想的,因为当我单独加载图像时,它总是能完美运行。
  • 所以你必须找出你达到的什么限制。如果它不是真正可复制的,那么它可能是两个或多个请求的冲突,可能来自同一个客户端并行加载一些图像。在触发请求时观察系统内存使用情况。尽管如果您以动态方式提供图像而不是静态文件,这应该只是一个瓶颈。

标签: php image error-handling


【解决方案1】:

检查 memory_limit ini php.ini 并在需要时提高。还要确保使用 ob_get_level() 关闭输出缓冲。如果打开,在 readfile() 之前调用 ob_end_flush()。

【讨论】:

  • 其实用 ob_start("callback", 1024*1024);似乎已经解决了这个问题。
  • 你在 callback() 里面放了什么?尝试在 readfile() 之前调用 ob_end_flush() 以防有人打开缓冲。
  • 我只是把 return $buffer 放在那里。此外,似乎我有时仍能获得 500,只是现在很少了。我仍然希望脚本在失败时重试。
  • 它永远不会失败。您是否尝试过我在之前评论中的建议?
猜你喜欢
  • 2011-05-08
  • 2022-01-07
  • 2018-05-25
  • 2023-03-20
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多