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