Yevgeni Posted September 28, 2010 Share Posted September 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214630-php-in-windows-returning-a-rn-when-processing-download-handler/ Share on other sites More sharing options...
Yevgeni Posted September 28, 2010 Author Share Posted September 28, 2010 Also: One solution I'm working on right now is to just buffer all of the data, remove the line break if it exists, then send the data that way. I'll update you if I solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/214630-php-in-windows-returning-a-rn-when-processing-download-handler/#findComment-1116796 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2010 Share Posted September 28, 2010 You didn't indicate if the \r\n was at the start of the data or at the end. The problem is likely a new-line in your download .php file, either before the opening <?php tag or after the closing ?> php tag. Quote Link to comment https://forums.phpfreaks.com/topic/214630-php-in-windows-returning-a-rn-when-processing-download-handler/#findComment-1116798 Share on other sites More sharing options...
Yevgeni Posted September 28, 2010 Author Share Posted September 28, 2010 Sorry, didn't specify that. It prepends the \r\n to the beginning of the data. Where would the newline be coming from? The script I posted generates no newline characters... Quote Link to comment https://forums.phpfreaks.com/topic/214630-php-in-windows-returning-a-rn-when-processing-download-handler/#findComment-1116800 Share on other sites More sharing options...
Yevgeni Posted September 28, 2010 Author Share Posted September 28, 2010 Well mark this one as solved! I still am not quite sure where this line break was coming from, but I buffered the entire output of the page and removed the initial line break using ltrim. That seemed to work without corrupting any of the binary data. Quote Link to comment https://forums.phpfreaks.com/topic/214630-php-in-windows-returning-a-rn-when-processing-download-handler/#findComment-1116869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.