Jump to content

PHP in Windows returning a \r\n when processing download handler.


Yevgeni

Recommended Posts

Here's a very interesting conundrum that I think will stump quite a few of you.

 

Here's the setup:

 

I was put in charge of the project of maintaining and developing a database application for one of my company's customers. The application has been evolving ever since. One of the more recent changes we've been working on is to add file upload functionality to catalog forms, letters, etc. I had this application working before by uploading the file directly to the server, giving it a random name, putting it in a /forms directory, and entering a hyperlink in a log line, which was then entered into the MySQL database. It's a pretty straightforward procedure. BUT my customer doesn't always have net access so he has a laptop running the application with its own MySQL and PHP installation, and he has a script that synchronizes the database with the one on our server. Not very practical or efficient, I know, but I inherited this project and had no choice in the matter. Since the client needs to upload and catalog files, and needs those files to be in sync with our server, I figured instead of just having a folder full of files that also has to be synched, why not throw binary data into the database and have the files in the database? Well I quickly developed a set of scripts to handle this task, and all of them worked flawlessly on our Linux server.

 

It worked, that is, until I tried to install the same code on our client's Windows crapbox. At this point, I'd like to convey the download script that I wrote:

 

require_once('includes/application.inc.php');
if($_REQUEST['id'])
{
$id = mysql_real_escape_string($_REQUEST['id']);
}
else die("Error");
$result = mysql_fetch_assoc(mysql_query("SELECT * FROM Files WHERE id = $id"));
header("Content-type: ".$result['type']);
header("Content-length: ".$result['size']);
header("Content-Disposition: attachment; filename=".$result['filename']);

echo $result['data'];

 

You see? Very simple and to the point. This script is called from a hyperlink elsewhere in the application, it queries for the file and downloads it. The problem is that when I'm running this download script on the laptop's Windows server, the browser echoes a \r\n as well as the binary data. As you can imagine, this wreaks havoc with files downloaded. Here's what I've determined:

 

  • The data IS being stored properly in the database
  • This only happens with certain files and not with others. The most egregious offenders are .odt files.
  • The data is not being returned from the database corrupted. I verified this using various regex's and by dumping the data right out of the database into hex in the browser.
  • Apache is not inserting that line break as seen by testing with static html files

 

From these things, I have determined that PHP is the most likely culprit. My limited knowledge of the entire library of functions notwithstanding, is there anything I'm missing? Is there a more "safe" way to echo binary data? Does anyone have any sort of clue as to what could be causing this? I've scoured Google and this forum and haven't found anything.

 

Thanks in advance for your insight.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.