Jump to content

Download 2 files from database that has one user id


brucensal

Recommended Posts

Hi,

 

Can anyone shed some light on this code for me.  I have managed to get a form to upload multiple images into 1 databse record per client.

 

I am now trying to download the uploaed files so i can work with them.

 

The code below works to a point i can view both files but when i click on the to download it only downloads to first file even though it does list the correct file names.

 

If you could help me with this it would be greatly appreciated.

 

<?
if(isset($_GET['id']))
{
// Details for webhost below:////////////
////////////////////////////////////////

$con = mysql_connect("localhost","****","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  

mysql_select_db("*****", $con);

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, content FROM ***** WHERE id = '$id'";
    $query2   = "SELECT name2, type2 ,size2, content2 FROM ***** WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
$result2  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);
list($name2, $type2, $size2, $content2) = mysql_fetch_array($result2);
    
header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;
header("Content-Disposition: attachment; filename=$name2");
    header("Content-length: $size2");
    header("Content-type: $type2");
    echo $content2;

}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
font-family: BankGothic Lt BT;
font-size: 12px;
}
-->
</style></head>

<body>
<?
// Details for webhost below:////////////
////////////////////////////////////////

$con = mysql_connect("localhost","******","*****l");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  

mysql_select_db("*******$con);
$query = "SELECT COUNT(id) AS numrows FROM *****";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$query  = "SELECT id, name, name2 FROM *****";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "<br>";
echo "<br>";
echo "$numrows <b>Download(s)in our database</b>";
echo "<br>";
echo "<br>";
    echo "Database is empty <br>";
}
else
{
echo "<br>";
echo "<br>";
echo "$numrows <b>Download(s)in our database</b>";
echo "<br>";
echo "<br>";
    while(list($id, $name, $name2) = mysql_fetch_array($result))

    {
?>
    <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<a href="download.php?id=<?=$id;?>"><?=$name2;?></a> <br>
<?        
    }
}
    mysql_close($con);
?>
</body>
</html>

EDITED BY WILDTEEN88: Please use code tags (


) when posting code within a post. Thank you

 

Link to comment
Share on other sites

You cannot use the same header multiple times. Only one or the other will be received, usually the first. The others will be ignored.

 

What you'll want to do is is perhaps get the files out the database then create an archive file (zip/rar etc) to store them in. Then you'd use 1 header for downloading the archive file.

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.