Jump to content

PHP Force Downloads Spits Up Text Garbage


GreenSmurf

Recommended Posts

Trying to get my force downloads script to work. The example below was actually taken from another script and modified but its similar to the one I originally wrote.

if($_REQUEST['download'] && $forcedownloads) {

$filename = urldecode($_REQUEST['download']);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);

exit(0);
}

 

It is pretty basic but the problem occurs when the script outputs this:

ÿØÿà�JFIF�,,��ÿá^Exif��II*���� �����†���� ���Œ�������š�������¢���(�����ÊÊ1����ª���2����¾��������ÎÈi‡����Ò���¤������ÍÒL��Canon�MX700 series��,�����,�����MP Navigator EX 1.0�2009:08:25 13:24:21�������0221����8��‘������ ����0100 �����š; ������ �������£����EX ����2009:08:25 13:24:21�������wJ�

 

Any help would be appreciated. Thank you.

 

-GS

This URL pointed me in the right direction. http://www.boutell.com/newfaq/creating/forcedownload.html I hope it helps someone else. So, what I did based on this was I changed this:

if($_REQUEST['download'] && $forcedownloads) {



$filename = urldecode($_REQUEST['download']);



header("Pragma: public");



header("Expires: 0");



header("Cache-Control: must-revalidate, post-check=0, pre-check=0");



header("Content-Type: application/force-download");



header("Content-Type: application/octet-stream");



header("Content-Type: application/download");



header("Content-Disposition: attachment; filename=".basename($filename).";");



header("Content-Transfer-Encoding: binary");



header("Content-Length: ".filesize($filename));



@readfile($filename);



exit(0);
}

to this:

$fname = $_REQUEST['download'];
echo "<script type=\"text/javascript\">";
echo "window.open(\"gen.php?download=$fname\",\"$fname\", \"resizable,toolbar,scrollbars,menubar,\");";
echo "</script>";

And made gen.php look like this:

<?php
$allowed_ext = array (
'zip' => 'application/zip',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/msword',
'docm' => 'application/msword',
'doct' => 'application/msword',
'dotm' => 'application/msword',
'dotx' => 'application/msword',
'dot' => 'application/msword',
'odt' => 'application/msword',
'wpd' => 'application/msword',
'wps' => 'application/msword',
'prn' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'xl' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.ms-excel',
'xlsm' => 'application/vnd.ms-excel',
'xlsb' => 'application/vnd.ms-excel',
'xlam' => 'application/vnd.ms-excel',
'xltx' => 'application/vnd.ms-excel',
'xltm' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'xla' => 'application/vnd.ms-excel',
'xlm' => 'application/vnd.ms-excel',
'xlw' => 'application/vnd.ms-excel',
'qba' => 'application/x-qbmsxml',
'qbb' => 'application/x-qbmsxml',
'qbm' => 'application/x-qbmsxml',
'qbw' => 'application/x-qbmsxml',
'qbx' => 'application/x-qbmsxml',
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg'

);

$filename = urldecode($_REQUEST['download']);

$fext = strtolower(substr(strrchr($filename,"."),1));

if (!array_key_exists($fext, $allowed_ext)) {
	die("Not allowed file type."); }

if ($allowed_ext[$fext] == '') {
	$mtype = '';
	if (function_exists('mime_content_type')) {
		$mtype = mime_content_type($file_path);}
	else if (function_exists('finfo_file')) {
		$finfo = finfo_open(FILEINFO_MIME);
		$mtype = finfo_file($finfo, $file_path);
		finfo_close($finfo);  
	}
	if ($mtype == '') {
		$mtype = "application/force-download";
	}
}else{
	$mtype = $allowed_ext[$fext];}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/$mtype");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
exit(0);
?>

And like magic the script works! I knew I was not crazy but eventually we all have our tipping point. Thanks to anyone who PM'd me or even considered my problem.

 

-GS

This URL pointed me in the right direction. http://www.boutell.com/newfaq/creating/forcedownload.html I hope it helps someone else. So, what I did based on this was I changed this:

if($_REQUEST['download'] && $forcedownloads) {



$filename = urldecode($_REQUEST['download']);



header("Pragma: public");



header("Expires: 0");



header("Cache-Control: must-revalidate, post-check=0, pre-check=0");



header("Content-Type: application/force-download");



header("Content-Type: application/octet-stream");



header("Content-Type: application/download");



header("Content-Disposition: attachment; filename=".basename($filename).";");



header("Content-Transfer-Encoding: binary");



header("Content-Length: ".filesize($filename));



@readfile($filename);



exit(0);
}

to this:

$fname = $_REQUEST['download'];
echo "<script type=\"text/javascript\">";
echo "window.open(\"gen.php?download=$fname\",\"$fname\", \"resizable,toolbar,scrollbars,menubar,\");";
echo "</script>";

And made gen.php look like this:

<?php
$allowed_ext = array (
'zip' => 'application/zip',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/msword',
'docm' => 'application/msword',
'doct' => 'application/msword',
'dotm' => 'application/msword',
'dotx' => 'application/msword',
'dot' => 'application/msword',
'odt' => 'application/msword',
'wpd' => 'application/msword',
'wps' => 'application/msword',
'prn' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'xl' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.ms-excel',
'xlsm' => 'application/vnd.ms-excel',
'xlsb' => 'application/vnd.ms-excel',
'xlam' => 'application/vnd.ms-excel',
'xltx' => 'application/vnd.ms-excel',
'xltm' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'xla' => 'application/vnd.ms-excel',
'xlm' => 'application/vnd.ms-excel',
'xlw' => 'application/vnd.ms-excel',
'qba' => 'application/x-qbmsxml',
'qbb' => 'application/x-qbmsxml',
'qbm' => 'application/x-qbmsxml',
'qbw' => 'application/x-qbmsxml',
'qbx' => 'application/x-qbmsxml',
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg'

);

$filename = urldecode($_REQUEST['download']);

$fext = strtolower(substr(strrchr($filename,"."),1));

if (!array_key_exists($fext, $allowed_ext)) {
	die("Not allowed file type."); }

if ($allowed_ext[$fext] == '') {
	$mtype = '';
	if (function_exists('mime_content_type')) {
		$mtype = mime_content_type($file_path);}
	else if (function_exists('finfo_file')) {
		$finfo = finfo_open(FILEINFO_MIME);
		$mtype = finfo_file($finfo, $file_path);
		finfo_close($finfo);  
	}
	if ($mtype == '') {
		$mtype = "application/force-download";
	}
}else{
	$mtype = $allowed_ext[$fext];}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/$mtype");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
exit(0);
?>

And like magic the script works! I knew I was not crazy but eventually we all have our tipping point. Thanks to anyone who PM'd me or even considered my problem.

 

-GS

 

In the gen.php I forgot this little important line of code and thought I should include it just in case anyone ever decides to use this as a learning tool. Just before the headers there needs to be a line of code that converts the $filename variable back into URL format. Hence this line of code:

$filename = urlencode($filename);

 

-GS

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.