downloading files from a database via web page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

downloading files from a database via web page

Post 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.

:(
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

anyone ?

lol
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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)?
Last edited by McGruff on Sat Nov 01, 2003 8:21 pm, edited 1 time in total.
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
bobby
Forum Commoner
Posts: 27
Joined: Sat Oct 04, 2003 4:00 pm
Location: usa
Contact:

Post by bobby »

I was suspected someone might bite at what i said.......lol


oh well
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
Post Reply