Jump to content

File download problem


justsomeguy
 Share

Recommended Posts

I'm troubleshooting an issue with file downloads, and I've just about exhausted my options. I was hoping to get some other people thinking about possible causes.I've got three versions of an application installed, the "stable", "next", and "dev" versions. The stable and next versions both use the same database, the dev version uses its own copy. I'm seeing a problem in the next version that doesn't appear in the stable or dev versions. I've got a script for downloading files which are saved in a database in a blob field. The files are email attachments, the script is for downloading an attachment to the email.The problem is that, in the next version, the file data is preceded by two space characters. This means that after all of the headers in the server response, and the blank line, there are two space characters before the file data (response body) starts. Obviously, I haven't been able to figure out where those characters are coming from. I've compared the download script and all included files between the next and dev versions, and other than a couple superficial changes in one of the included files they are all identical. That seems to indicate it's not a problem with the code. I've verified that the data in the database does not start with two space characters, and since the stable and next versions use the same database, and the stable version works, that indicates that there's not a problem with the data in the database. I've also verified that the same thing happens with multiple files and types (e.g., images vs audio).This is the code for the download script, if anyone wants to see the included files let me know, but there are about 10 of them.

<?phprequire_once 'include/global.conf.php';if (!$sess->userid){  exit('You must log in to use this.');}$email = intval(form_var('email'));$file = form_var('file');$db->sql('SELECT filename, data FROM email_attachments WHERE email_id=%d AND filename=%s');$db->add_param($email, false);$db->add_param($file);$file = $db->select();if (!$file){  error_log('The file ' . $file . ' was not found.');  exit('The file ' . $file . ' was not found.');}$mime = mime_content_type($file[0]['filename']);$file[0]['data'] = utf8_decode($file[0]['data']);header('Content-type: ' . $mime);header('Content-length: ' . strlen($file[0]['data']));header('Content-disposition: attachment; filename="' . $file[0]['filename'] . '"');echo $file[0]['data'];exit();?>

So, you can see that I send the content headers, including content-length, followed by the data. Here's what really screws me up: the content-length header contains the correct length of the data, but the data actually sent is two characters longer. That means that $file[0]['data'] contains the correct data, but apparently somewhere between the content-disposition header and the echo statement, two spaces go out. I'm not seeing any errors related to this, including header errors, so by the time I send those headers no output has been sent. It looks like the server is pre-pending the two spaces to the response, but I don't understand why it would do that in one installation and not the others. All three installations are on the same server in different directories. The server is a Windows Server 2008 R2 machine.Something else I just noticed: it appears that all of my JSON responses in the "next" version also have the two extra spaces. Static files like Javascript files do not have the spaces. The index.php file does have the two spaces. The JSON responses in the dev version do not have the extra spaces. The script sending JSON responses includes the same files as the file download script.I'm not real sure what else to say, if anyone has any suggestions or questions or wants to see other files or settings please let me know.

Link to comment
Share on other sites

man, I wish I could help you. I've got a similar problem in mobile web app I'm developing where for some reason after saving a newly composed message to the 'Drafts' folder and then viewing it again, the message keeps popping back out with a trailing ). grrr.....

Link to comment
Share on other sites

It looks like this is working now, but I still don't know what the problem was. I re-saved two include files with different encodings, and that fixed it, but I haven't been able to save to an encoding to get it to break again. Regardless, if this was a UTF BOM issue or something then I should have gotten a header error. Confusing...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...