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