Jump to content

PHP Download Excel File - Encoding Problem


fmpros

Recommended Posts

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();

}

}
?> 

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?

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]

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

 

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.