Jump to content

Downloadable File


schalk

Recommended Posts

Hello Everyone,

 

I am busy working on a project which needs to be able to download a file from a URL which has a certain fileID. For example if someone does "http://www.mysite.com/download.php?fileID=1" then it does exactly the same as if you were to go to "http://www.mysite.com/download/file1.zip" and if instead you did "http://www.mysite.com/download.php?fileID=2" then it would do exactly the same as "http://www.mysite.com/download/file2.zip". How do I achieve this?

Link to comment
https://forums.phpfreaks.com/topic/142295-downloadable-file/
Share on other sites

use $_GET to get the id then get the file name from the db, then send the file as a header

 

e.g.

$id = $_GET['id'];
$sql = mysql_query("SELECT filename FROM table WHERE id = '".mysql_real_escape_string($id)."' LIMIT 1") or die(mysql_error());
$sql = mysql_fetch_assoc($sql);
header("Location:".$sql['filename']);

Link to comment
https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745571
Share on other sites

use $_GET to get the id then get the file name from the db, then send the file as a header

 

e.g.

$id = $_GET['id'];
$sql = mysql_query("SELECT filename FROM table WHERE id = '".mysql_real_escape_string($id)."' LIMIT 1") or die(mysql_error());
$sql = mysql_fetch_assoc($sql);
header("Location:".$sql['filename']);

 

Ahh so header(); was the magic function I was looking for, thanks!

Link to comment
https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745575
Share on other sites

That might just redirect to the file page, or it might not i am not %100.

 

This is my code for downloading zip files.

 

<?php
if(isset($_GET['download']) && is_numeric($_GET['download']))
{
$id = $_GET['download'];
$id = stripslashes($id);
$sql = "SELECT * FROM djw_snippet WHERE id = ".$id;
$sql = mysql_query($sql);
$count = mysql_num_rows($sql);
if($count == 0) 
{
	header("Location:http://djw-webdesign.awardspace.com/snippet.php");
}
else
{
	$row = mysql_fetch_assoc($sql);
	$file = $row['file'];
		if(!file_exists("Files/".$file))
	{
		$sq1 = "INSERT INTO djw_missing_file (file,ip) VALUES ('".$file."','".$_SERVER['REMOTE_ADDR']."')";
		$sq1 = mysql_query($sq1);
		?>
		<script type="text/javascript">
			alert("The file you are trying to download cannot be found. This error has been reported to an administrator.");
		</script><noscript>
			The file you are trying to download cannot be found. This error has been reported to an administrator.<br>
			You will now be redirected.
		</noscript>



		<meta http-equiv="refresh" content="3;url=http://djw-webdesign.awardspace.com/snippet.php">			
		<?php
	}
	else
	{
		$downs = $row['downloads'] + 1;
		mysql_query("UPDATE djw_snippet SET downloads = '$downs' WHERE id = '$id'");
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/force-download");
		header( "Content-Disposition: attachment; filename=".basename($file));	
		header( "Content-Description: File Transfer");
		readfile($file);
	}
}	
}
?>

Link to comment
https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745577
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.