我使用PHP脚本将视网膜图像服务器端显示到具有高分辨率显示的设备时,通常会出现错误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

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

最佳答案

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

关于php - PHP镜像脚本经常给出错误500内部服务器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14913775/

10-12 07:27