Page 1 of 1

downloading files from a database via web page

Posted: Fri Oct 31, 2003 6:47 pm
by bobby
Hi guys

I have manged to get a tutorial file working to some degree. I'm just stcuk on one bit of it. after uplaoding my file to my database, i then want to be able to download via webpage. My code doesnt seem to work. Can anyone see why?

This is the file main.php

Code: Select all

<?php
<?php
include "open_db.php";

$sql = "SELECT * FROM tbl_files ";
$sql .= "ORDER BY filename ASC";
$result = mysql_query($sql, $db); //result = mysql_query($sql, $db) or exit(mysql_error());
$rows = mysql_num_rows($result);

echo "<table>\n";
echo " <tr>\n";
echo "  <td>Filename</td>\n";
echo "  <td>Type</td>\n";
echo "  <td>Size</td>\n";
echo "  <td>Description</td>\n";
echo "  <td> </td>\n";
echo " </tr>\n";

for ($i = 0; $i < $rows; $i++) {
  $data = mysql_fetch_object($result);
  //print_r($data); 
  // since our script is very small, i'm not going to escape out to html mode here
  echo " <tr>\n";
  echo "  <td>$data->filename</td>\n";
  echo "  <td>$data->filetype</td>\n";
  echo "  <td>$data->filesize</td>\n";
  echo "  <td>" . stripslashes($data->description) . "</td>\n";
  echo "  <td>( <a href='download.php?id=".$data->id_Files."'>Download</a> )</td>\n";
  echo " </tr>\n";
}
mysql_free_result($result);
mysql_close($db);
?>


?>
This is the file download.php

Code: Select all

<?php
<?php
if (isset($_GET['id'])) {
 include("open_db.php");
  $sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_files WHERE
id_files=$_GET['id']";
	
  $result = mysql_query($sql, $db);
  $data = mysql_fetch_object($result);  
	
  header("Content-type: $data->filetype");
  header("Content-length: $data->filesize");
  header("Content-Disposition: attachment; filename=$data->filename");
  header("Content-Description: PHP Generated Data");
  echo $data->bin_data;
}
?>



?>

at the momment when i click on the download link to downlaod the file. I just see a blank page.

:(

Posted: Sat Nov 01, 2003 2:53 pm
by bobby
anyone ?

lol

Posted: Sat Nov 01, 2003 5:21 pm
by McGruff
What do you get if you turn the ..or die(mysql_error()) back on?

I'd recommend you always do this, while developing.

Posted: Sat Nov 01, 2003 6:01 pm
by bobby
I get a blank page.

Do you know of any chatrooms where i could go to get php support or are u involved in any mr moderator.

gotta keep things moving

cheers

Posted: Sat Nov 01, 2003 6:22 pm
by McGruff
Don't know of any php chatrooms. Most posts on this board do get answered though.

To debug, run through the script using echo / die() / printr() to check that vars /arrays / objects are getting the values you expect - if indeed they are set at all (turn error reporting up to E_ALL if you haven't already).

What do you get when you echo $rows, and when you print_r($data)?

Posted: Sat Nov 01, 2003 6:42 pm
by bobby
Hi am about to go to bed but will try what u suggested tomorrow.

Cheers

Regarding the boards, i have posted here before, and people do respond but my experience is that nothing has ever been resolved fully for me.

I have been in to chatrooms but i cant remmber the irc network and cant find it on through the search engines either. The chat room was full of php developers who helped straight away and resloved problems. I think on discussion boards its oftne a slow process i mean today my post nealry went over to the next page, but i typed a little message to make is saty on the first page other wise i doubt people would have seen it.

Posted: Sat Nov 01, 2003 7:27 pm
by Gen-ik
bobby wrote: Regarding the boards, i have posted here before, and people do respond but my experience is that nothing has ever been resolved fully for me.
With all due respect you've probably noticed that this IS NOT a chatroom, and if you didn't come across as being so impatient you might get more help from people. We will help you out when we have the time (and patients) to do so, not when you say jump.

Posted: Sun Nov 02, 2003 7:19 am
by bobby
I was suspected someone might bite at what i said.......lol


oh well

Posted: Sun Nov 02, 2003 7:06 pm
by m3mn0n
Yeah, I recommend reading the stickies in this forum if you want the most out of this forum's support system.

Post error messages and code and what exactally you need done and what is happening will usually get you responces and resolutions to your issues.

Regards.