fmpros Posted July 12, 2011 Share Posted July 12, 2011 Hi Folks, I'm having trouble with the attached script when it comes to downloading excel files. The encoding appears to be incorrect (the text in the downloaded file is garbled). The excel files are not generated via php. They are simply uploaded by the client via FTP. What am I doing wrong? Any help would be greatly appreciated! Thanks Much, John <?php $hiddenpath = "/website/myhiddenpath/download/"; // VARIABLES if (!empty($_GET['file'])){ $file = base64_decode($_GET['file']); }else{ echo "Missing Required Data"; die(); } $ext = '.xls'; $file_real = $hiddenpath.$file.$ext; $ip = $_SERVER['REMOTE_ADDR']; /*echo $file_real; die();*/ // Check to see if the download script was called if (basename($_SERVER['PHP_SELF']) == 'dl.php' ) { //If requested file exists if (file_exists($file_real)){ // Get extension of requested file $extension = strtolower(substr(strrchr($file, "."), 1)); echo $extension; // Determine correct MIME type switch($extension){ case "asf": $type = "video/x-ms-asf"; break; case "avi": $type = "video/x-msvideo"; break; case "exe": $type = "application/octet-stream"; break; case "mov": $type = "video/quicktime"; break; case "mp3": $type = "audio/mpeg"; break; case "mpg": $type = "video/mpeg"; break; case "mpeg": $type = "video/mpeg"; break; case "rar": $type = "encoding/x-compress"; break; case "txt": $type = "text/plain"; break; case "wav": $type = "audio/wav"; break; case "wma": $type = "audio/x-ms-wma"; break; case "wmv": $type = "video/x-ms-wmv"; break; case "zip": $type = "application/x-zip-compressed"; break; case "xls": $type = "application/vnd.ms-excel;charset:UTF-8"; break; default: $type = "application/force-download"; break; } // Fix IE bug [0] $header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1) : $file.$ext; // Prepare headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public", false); header("Content-Description: File Transfer"); header("Content-Type: " . $type); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"" . $header_file . "\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($file_real)); readfile($file_real); }else{ // Requested file does not exist (File not found) echo("Requested file does not exist"); die(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/ Share on other sites More sharing options...
requinix Posted July 12, 2011 Share Posted July 12, 2011 The $type is wrong; should be simply application/vnd.ms-excel XLS files are not text/* types so should not have a character encoding (which was specified incorrectly anyways). Also remove the Accept-Ranges:bytes header you send. Your script doesn't support it so don't claim that it does. How garbled? Can Excel not open them? Does it but the data is messed up? Only quotes and some other characters are wrong? Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1241931 Share on other sites More sharing options...
btherl Posted July 12, 2011 Share Posted July 12, 2011 If what requinix suggested doesn't fix it, I would check that you are using binary (aka image) mode when uploading the excel files, and not text mode. That's a common problem with FTP. Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1241979 Share on other sites More sharing options...
fmpros Posted July 12, 2011 Author Share Posted July 12, 2011 Thanks for the replies, much appreciated. The FTP client is uploading in binary mode. I tried making the changes requinix suggested and I'm still having problems. As for what is coming across "garbled", basically the whole document. I've attached two screenshots that show the correct and actual output. Thanks again for the input guys. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1242001 Share on other sites More sharing options...
requinix Posted July 12, 2011 Share Posted July 12, 2011 Can you make a small example spreadsheet, run it through your process, then upload the two versions somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1242013 Share on other sites More sharing options...
fmpros Posted July 12, 2011 Author Share Posted July 12, 2011 The file 2011-0014 is a sample I put together. This is what I get when I download the file via FTP. The "Corrupt.xls" version is what I get when I use the script to download. Thanks for taking a look. BTW, I had to use mediafire. The forum doesn't allow xls. Valid: http://www.mediafire.com/?dkmgtsbz24b26mn Invalid:http://www.mediafire.com/?em667h4sgz2ugub Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1242025 Share on other sites More sharing options...
requinix Posted July 12, 2011 Share Posted July 12, 2011 There are two newlines at the beginning of the file that shouldn't be there. Removing them fixes the file. Check for any whitespace before opening <?php tags or after closing ?> tags. Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1242037 Share on other sites More sharing options...
fmpros Posted July 13, 2011 Author Share Posted July 13, 2011 I figured out the problem. I hope this helps someone down the line. Adding "ob_clean();" corrected the problem! See below: header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public", false); header("Content-Description: File Transfer"); header("Content-Type: " . $type); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"" . $header_file . "\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($file_real)); ob_clean(); readfile($file_real); Requinix - Thanks lending a hand! Quote Link to comment https://forums.phpfreaks.com/topic/241823-php-download-excel-file-encoding-problem/#findComment-1242336 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.