pcman Posted February 6, 2008 Share Posted February 6, 2008 how can i do that if i go to the download.php page its download me a file(mp3 file)? i know that i need use in header but how can i do that? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/89813-download-files-php/ Share on other sites More sharing options...
Stooney Posted February 6, 2008 Share Posted February 6, 2008 I'll just give you a download.php I have that I know works. <?php error_reporting(0); session_start(); include("../dtf_dbc.php"); //This is the database connection $fileid=mysql_real_escape_string($_GET['fileid']); $get=mysql_query("SELECT fname, path FROM dtf_files WHERE id='$fileid'"); //this retrieves the file path from the database if(mysql_num_rows($get)==1){ $row=mysql_fetch_array($get); $basefilename=$row[0]; $filename=$row[1].$row[0]; if(false !== ($fh = fopen($filename, 'r'))){ header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); $ext=strtolower(substr($basefilename,strlen($basefilename)-3, 3)); if ($ext == "mp3" ) { header("Content-Type: audio/x-mp3"); } else if ($ext == "txt") { header("Content-Type: text/plain"); } else if ($ext == "jpg") { header("Content-Type: image/jpeg"); } else if ($ext == "gif") { header("Content-Type: image/gif"); } else if ($ext == "png") { header("Content-Type: image/png"); } else if ($ext == "swf") { header("Content-Type: application/x-shockwave-flash"); } else if ($ext == "flv") { header("Content-Type: video/flv"); } else { header("Content-type: application/octet-stream"); } header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); fclose($filename); } else{ echo '<div class="error">Failed opening file!</div>'; } } else{ echo '<div class="error">Failed retrieving file information from database!</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89813-download-files-php/#findComment-460256 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.