Jump to content

Downloading from server or database


mamavi

Recommended Posts

hi everyone. i have a problem downloading files uploaded into mysql database. The code snippet i have displays the files as links, however when this is clicked. i get the forbidden message(error 404).

i will like to know of a way to view files uploaded onto the apache server.

Anyway, below is the code snippet for the download: ??? ???

<?php

 

include "dbconn.php";

mysql_select_db("Library");

$query = "SELECT id, name FROM PastQuestions";

$result = mysql_query($query) or die("not selected");

if(mysql_num_rows($result) == 0) {

echo "database empty<br/>"; }

else{

while(list($id,$name) = mysql_fetch_array($result)) {

?>

<a href=\"download.php?id <?php echo $id;?>\"><?php echo $id.".<nbsp><nbsp>".$name;?></a><br/>

<?php

}

}

 

?>

 

 

<?php

if(isset($_GET['id']))

{

include "dbconn.php";

mysql_select_db("Library") or die("Error selecting database");

$id = $_GET['id'];

$query = "select name, type, size, content from PastQuestions where id = '$id'";

$result = mysql_query($query) or die ("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;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/135921-downloading-from-server-or-database/
Share on other sites

My first guess is that you're missing the = in your href='...'

 

<a href=\"download.php?id=<?php echo $id;?>\"><?php echo $id.".<nbsp><nbsp>".$name;?></a><br/>

 

Also, you're going to have to wrap your code before the if(GET...) to only show if GET doesn't exist, otherwise you're going to get some header errors when that condition evaluates true...

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.