ToonMariner Posted May 31, 2007 Share Posted May 31, 2007 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 https://forums.phpfreaks.com/topic/53736-file-download/ Share on other sites More sharing options...
Daniel0 Posted May 31, 2007 Share Posted May 31, 2007 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 https://forums.phpfreaks.com/topic/53736-file-download/#findComment-265592 Share on other sites More sharing options...
ToonMariner Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks but no luck - think I may have to put a ticket in on server admin.... Link to comment https://forums.phpfreaks.com/topic/53736-file-download/#findComment-265649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.