Jump to content

Small problem with PHP file download


gnawz

Recommended Posts

Guys,

 

I upload my files with the following scripts which work pretty well...

 

<?
unction addProdContent()
{
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];

	$fp      = fopen($tmpName, 'r');
	$content = fread($fp, filesize($tmpName));
	$content = addslashes($content);
	fclose($fp);

	if(!get_magic_quotes_gpc())
	{
		$fileName = addslashes($fileName);
	}

	$sql = "SELECT * FROM mogas_productscontent WHERE Name = '$fileName'";
	$result = dbQuery($sql);

	if (dbNumRows($result) == 1) 
	{
		header('Location: index.php?view=add&error=' . urlencode('File already uploaded. Choose another one'));	
	} 
	else
	{

	$query = "INSERT INTO mogas_productscontent (Name, Size, Type, Content, DateAdded ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content', Now())";

	mysql_query($query) or die('Error, query failed');
	}
}

header('Location: index.php');	
}
?>

 

My problem is downloading the files. I mean for a user to click on a link and get a Save As Window.

 

How do I achieve this?

 

This is the script I am using currently...

<?
require_once 'mogas_utils.php';

//require_once'template.php';

//pgheader('MGS International - Downloads');


$query = "SELECT * FROM mogas_productscontent";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($productscontentid, $Name) = mysql_fetch_array($result))
{
?>
<a href="d.php?id=<?php echo $productscontentid; ?>"><?php echo $Name; ?></a> <br>
<?php
}
}
?>


<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

$id    = $_GET['id'];

$query = "SELECT * FROM mogas_productscontent WHERE productscontentid = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($Name, $Type, $Size, $Content) = mysql_fetch_array($result);

header("Content-length: $Size");
header("Content-type: $Type");
header("Content-Disposition: attachment; filename=$Name");
echo $Content;
exit;
}

?>

 

What this does is attempt to open the PDF files in the same window with jumbled characters.

I want to download my files thus:

 

Give a user a link which they click to get a Save As dialogue

 

Please help.

 

 

 

Link to comment
Share on other sites

By what I'm guessing, You click the pdf link, and it then opens up a dialog box with one button saying 'save as' ?

 

I don't understand what it is you are trying to do what the 'user' clicks the link for download.

Link to comment
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.