Friday, November 25, 2011

♫ DOWNLOAD FILE FROM DB

we need two php file (a) file_download.php and (b) file_download2.php (make sure you have 'upload' directory from where the files would be downloaded)




(a) file_download.php

<?php

$dbh = mysql_connect("localhost","root","") or die("There was a problem with the database connection.");

$dbs = mysql_select_db("databasename2", $dbh) or die("There was a problem selecting the categories.");

$result=mysql_query("SELECT * FROM table1 ");

echo "id \n\n";

echo "name \n\n";

echo "size(byte)\n\n";

echo "&lt;br>&lt;br>";

while($row=mysql_fetch_array($result))

{

echo "$row[id]"."\n\n\n\n";

echo "$row[name]"."\n\n\n\n";

echo "$row[size]";

echo " <td>" . stripslashes($row->description) . "</td>\n";

echo " <td>( &lt;a href='file_download2.php?id=$row[id]'>Download&lt;/a> )</td>\n";

echo " </tr>";

echo "<br><br>";

}

mysql_free_result($result);

?>


(b) file_download2.php

<?php

$dbc=mysql_connect("localhost","root","") or die("error in db connection");

$dbs=mysql_select_db("databasename2",$dbc) or die("error in db selection");

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

{

$id = $_GET['id'];

$query = "SELECT name, type, size, path FROM table1 WHERE id = '$id'";

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

list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-length: $size");

header("Content-type: $type");

header("Content-Disposition: attachment; filename=$name");

header("Content-Description: PHP Generated Data");

readfile($filePath);

exit;

}

?>

( note: i was not able to download files with associated format with the above code,
after download, rename the file with its fomat, like- after download,
the audio file would be 'strings'
and you have to rename it as 'strings.mp3', thats all )

No comments:

Post a Comment