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 8559907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:09:39+00:00 2026-06-11T16:09:39+00:00

I’ve got an application running under Drupal in a LAMP environment. It delivers images

  • 0

I’ve got an application running under Drupal in a LAMP environment. It delivers images out of a database. On my personal machine (OS X Lion) it is working correctly. On the dev server (Amazon EC2) the exact same code doesn’t work. Both servers run PHP 5.3. I’ve confirmed that the data is getting corrupted between echo $fileData and it being received by the browser. Further, I’ve confirmed that the data is NOT corrupted if it is base64-encoded (I used a script to request the data, decode it, and save it to a file).

On the Drupal side, the code is in a module that creates a menu callback; the callback function echos the file data directly to avoid storing the entire image in memory (I was using PDO::FETCH_BOUND with PDO::PARAM_LOB to create a stream but it’s going into a string right now while I’m trying to find the problem; that wasn’t it). The menu entry has a custom delivery callback that essentially does nothing. I tried flushing any output buffering on the idea that Drupal might be trying too hard for Unicode support or something, but that didn’t help either.

I’m hoping that somebody has an idea of what might be causing my problem. If I haven’t been clear enough, I’ll post some code; right now it’s full of commented-out features so it would take some cleaning up.

Update 1:

I put together some example code but it did not exhibit the bug; I may convert the code into a Drupal module to test that code stack. However, I have narrowed down the bug to echo $fileData. The code looks something like this:

function example_menu() {
  $pages['mpicture/%'] = array(
    'title' => 'Picture handler',
    'page callback' => 'example_picture',
    'page arguments' => array(1),
    'delivery callback' => 'example_deliver_png',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
}

function example_picture($fileID) {
  // Point 1
  example_output_file($fileID);
}

function example_deliver_png($content) {
}

function example_output_file($fileID) {
  $statement = db_select('mfiles', 'f')
    ->fields('f', array('fileType', 'fileSize', 'fileData', 'fileData64', 'lastModified'))
    ->condition('fileID', $fileID, '=')
    ->execute();

  if ($file = $statement->fetchAssoc()) {
    header('Content-Type: ' . $file['fileType']);
    // Point 2
    header('Content-Length: ' . $file['fileSize']);
    echo $file['fileData'];
  }
}

This does not work. If I change Point 2 to this:

    // Point 2
    header('Content-Length: ' . $file['fileSize']*4/3);
    echo $file['fileData64'];

It works correctly, as long as my client runs a base64_decode on the output. However, if I do this:

  // Point 1
  ob_start()
  example_output_file($fileID);
  $output = base64_encode(ob_get_clean());
  header('Content-Length: ' . strlen($output));
  echo $output;

…it doesn’t work with the same base64_decodeing client. How could that possibly be?

And yes, I am storing the data twice; in every single test I run it doesn’t matter whether I use $file['fileData'] or base64_decode($file['fileData64']) so I don’t think it’s a database issue.

Update 2:

But curiously, this works:

  // Point 1
  ob_start()
  example_output_file($fileID);
  $output = base64_encode(trim(ob_get_clean()));
  header('Content-Length: ' . strlen($output));
  echo $output;

So, I guess now I try to find where the white space gets printed?

  • 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-11T16:09:40+00:00Added an answer on June 11, 2026 at 4:09 pm

    I’m going to strangle the intern. I swear it. Not really, it’s an expression…but I’m pissed.

    The entire problem was because a new file he added had white space at the end of it. A simple “?>\n”. This printed white space before every page on the server, including the image handler. The white space made the image corrupt.

    I guess the moral here is to never close PHP tags at the end of files, and make damn sure that everyone you work with does the same.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article

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.