Jump to content

download files - php


pcman

Recommended Posts

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>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/89813-download-files-php/#findComment-460256
Share on other sites

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.