Computing.Net > Forums > Web Development > Downloading a file from mysql

Downloading a file from mysql

Reply to Message Icon

Original Message
Name: martineagle
Date: December 19, 2005 at 00:29:41 Pacific
Subject: Downloading a file from mysql
OS: Win XP Pro
CPU/Ram: 512
Comment:

Hi there PHP geniuses.

I've just set up a test MySQL database and have written php pages to upload to it, read the contents of it, edit the contents of it and download files from it.

All these pages are working like a dream but the one thing I'm having trouble with is the downloading of any of the files (pics, word docos). Can you please tell me why the below script is causing the popup (the one that asks me if I'd like to save the file or run it) to only display the page name "download.php" rather than the file itself? and if I choose "Save" I get a file of zero KB (called download.php which is the page name)?

The other piece of information you may need to know is that the fsize column in my DB always shows 127, however the content column, which is of type mediumblob and attribute of BINARY (this is where I'm uploading the files to), has the correct file size in it. I use myphpadmin to view my DB.

This is the first page...
<?php
include("dbinfo.inc");
include("dbopen.inc");

$sql = "SELECT * FROM resource_info ";
$sql .= "ORDER BY fname ASC";
$result = mysql_query($sql, $dbh);
@$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>Title</td>\n";
echo " <td> </td>\n";
echo " </tr>\n";

for ($i = 0; $i < $rows; $i++) {
$data = mysql_fetch_object($result);
echo " <tr>\n";
echo " <td>$data->fname</td>\n";
echo " <td>$data->ftype</td>\n";
echo " <td>$data->fsize</td>\n";
echo " <td>" . stripslashes($data->title) . "</td>\n";
echo " <td>( Download )</td>\n";
echo " </tr>\n";

}
@mysql_free_result($result);
mysql_close($dbh);
?>

and when I click "Download" it runs this page...
<?php
if ($id) {
include("dbinfo.inc");
include("dbopendl.inc");
$sql = "SELECT fdata, ftype, fname, fsize FROM resource_info WHERE id=$id";

$result = @mysql_query($sql, $dbh);
$data = @mysql_result($result, 0, "fdata");
$name = @mysql_result($result, 0, "fname");
$size = @mysql_result($result, 0, "fsize");
$type = @mysql_result($result, 0, "ftype");

header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
}
?>

Thanks in advance everyone, you're always a big help :)

Martin


Report Offensive Message For Removal


Response Number 1
Name: martineagle
Date: December 19, 2005 at 04:10:52 Pacific
Reply: (edit)

UPDATE:
I figured out what I did wrong with the fsize column, I had it set to TINYINT which has a max value of 127 (NOOB!). I've changed this to INT and it's showing the correct file size now.

I've tested again since this minor fix and my other issue still exists.

Regs,

Martin



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Downloading a file from mysql

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge