PHP image script often gives error 500 internal server error

801

My PHP script to server retina images to devices with a high-resolution display often gives an error 500, and I have no idea why. This is my script:

<?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();
?>

Here's my error log:

[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

The error happens on random images in the page, it's always different ones. Sometimes the page even loads without any errors on my images.

684

Answer

Solution:

Check memory_limit ini php.ini and raise if needed. Also ensure output buffering is off with ob_get_level(). If on, call ob_end_flush() before readfile().

People are also looking for solutions to the problem: php - Wordpress - loop image description

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.