Jump to content

File download


ToonMariner

Recommended Posts

I have this code

 

if	(
strcmp(substr($_SERVER['DOCUMENT_ROOT'],-1,1),'/')	==	0
)
{
include_once($_SERVER['DOCUMENT_ROOT'] . 'config.php');
}
else
{
include_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
}



$error		=	NULL;
$user		=	$_GET['user'];
$filename	=	$user . $_GET['file'];

//echo $filename;

if	(
ini_get('zlib.output_compression')
)
{
ini_set('zlib.output_compression', 'Off');
}

$file_extension	=	explode('.',$filename);
$pos	=	count($file_extension) - 1;
//echo $pos;
$file_extension	=	$file_extension[$pos];
//echo $file_extension;

if	(
is_null($filename)
) 
{
$erro	=	 "Error: No file path passed to script.";
echo $erro;
exit;
}
/*
else
{
$first_char	=	substr($filename,0,1); 
if	(
	$first_char		==	"/"
	)
{ 
	$filename	=	substr(strtolower($filename), 1); 
}
if	(
	!file_exists($_SERVER['DOCUMENT_ROOT'] . $filename)
	) 
{
	$error 	=	"Error: No such file found.";
	echo $erro;
	exit;
}
}*/


switch	($file_extension)
{
case	"pdf":
	$ctype	=	"application/pdf";
	break;
case	"exe":
case	"mp3":
	$ctype	=	"application/octet-stream";
	break;
case	"zip":
	$ctype	=	"application/zip";
	break;
case	"doc":
	$ctype	=	"application/msword";
	break;
case	"xls":
	$ctype	=	"application/vnd.ms-excel";
	break;
case	"ppt":
	$ctype	=	"application/vnd.ms-powerpoint";
	break;
case	"gif":
	$ctype	=	"image/gif";
	break;
case	"png":
	$ctype	=	"image/png";
	break;
case	"mp3":
	$ctype	=	"audio/mp3";
	break;
case	"mpg":
case	"mpeg":
	$ctype	=	"video/mpeg";
	break;
case	"mp4":
	$ctype	=	"video/mp4";
	break;
case	"jpeg":
case	"jpg":
	$ctype	=	"image/jpg";
	break;
default:
	exit();
}


header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: ' . $ctype);
header('Content-Disposition: attachment; filename="' . basename($filename) . '";' );
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit();


?>

 

Which I pass the correct parameters through to download a file.

 

The file download is successful BUT when trying to play the file I get this from media player...

 

The file you are attemping to play has an extension (.mp3) that does not match the file format....

 

Can anybody suggest why this is so? I have removed id3 tags from the mp3 files etc etc and still no joy...

Link to comment
Share on other sites

This works for me:

<?php
$path = substr($_SERVER['DOCUMENT_ROOT'],-1)=='/' ? $_SERVER['DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT']."/";
require_once "{$path}config.php";

$error		= null;
$user		= $_GET['user'];
$filename	= $user.$_GET['file'];

if(ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compresion',0);
}

$file_parts = pathinfo($filename);
$file_extension = $file_parts['extension'];

if(empty($filename))
{
die('Error: No file path passed to script.');
}

$mime = array(
	'pdf' => 'application/pdf',
	'exe' => 'application/octet-stream',
	'zip' => 'application/zip',
	'doc' => 'application/msword',
	'xls' => 'application/vnd.ms-excel',
	'ppt' => 'application/vnd.ms-powerpoint',
	'gif' => 'image/gif',
	'png' => 'image/png',
	'mp3' => 'audio/mp3',
	'mpg' => 'video/mpeg',
	'mpeg'=> 'video/mpeg',
	'mp4' => 'video/mp4',
	'jpg' => 'image/jpg',
	'jpeg'=> 'image/jpg',
);

if(key_exists($file_extension,$mime))
{
$ctype = $mime[$file_extension];
}
else {
die();
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: {$ctype}");
header("Content-Disposition: attachment; filename={$file_parts['basename']}");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
header("Content-Length: ".filesize($filename));
readfile($filename);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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