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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Downloading a file from mysql

Reply to Message Icon

Name: martineagle
Date: December 19, 2005 at 00:29:41 Pacific
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



Sponsored Link
Ads by Google

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

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



0
Reply to Message Icon

Related Posts

See More


XML and Servlet RSS Reader



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Downloading a file from mysql

FTP Error www.computing.net/answers/webdevel/ftp-error/424.html

how can we know how many times a .. www.computing.net/answers/webdevel/how-can-we-know-how-many-times-a-/2672.html

Mozilla - no downloads www.computing.net/answers/webdevel/mozilla-no-downloads/2460.html