Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9305285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:53:39+00:00 2026-06-18T23:53:39+00:00

My PHP script to server retina images to devices with a high-resolution display often

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:53:40+00:00Added an answer on June 18, 2026 at 11:53 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script on a server <?php // retrieve POST vars $comment
I want to display an image using this php script on my server. $file
I have a PHP script running on my LAMP server that requires certain files
I have a PHP server script that SELECTs some data from a MySQL database.
I have a script on my server that dynamically creates images of chess diagrams:
I have a PHP script on a server, all working fine for most of
I now have a small java script server working correctly, called by: <?php $handle
I have to encrypt a connection between a PHP script one one server to
I have a Javascript function which calls a PHP script on my server. In
I have a PHP script on a server that generates the XML data on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.