0

Hi, I set up an upload page and a script to view files uploaded into a mySQL database. I'm using phpmyadmin and everything looks fine for the database, it's showing content in the BLOB field. My problem is that my download script isn't working. It should be bringing up a download dialog, however all I get is a blank browser page. I was hoping I would come across someone on here who could help. Below is the download script I'm using.

<?php
if ($id) {
  include "open_db.php";
  $sql = "SELECT name, type, size, content FROM upload WHERE id=$id";
	
  $result = @mysql_query($sql, $db);
  $data = @mysql_result($result, 0, "content");
  $name = @mysql_result($result, 0, "name");
  $size = @mysql_result($result, 0, "size");
  $type = @mysql_result($result, 0, "type");
	
  ("Content-type: $type");
  header("Content-length: $size");
  header("Content-Disposition: attachment; filename=$name");
  header("Content-Description: PHP Generated Data");
  echo $data;
}
?>

and here is the viewer

<?php
include "open_db.php";

$sql = "SELECT * FROM upload ORDER BY name ASC";
$result = mysql_query($sql, $db);
$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);
  echo " <tr>\n";
  echo "  <td>$data->name</td>\n";
  echo "  <td>$data->type</td>\n";
  echo "  <td>$data->size</td>\n";
  echo "  <td>" . stripslashes($data->description) . "</td>\n";
  echo "  <td>( <a href='download.php?id=$data->id'>Download</a> )</td>\n";
  echo " </tr>\n";
}
mysql_free_result($result);
mysql_close($db);
?>

also, here is the upload code just in case.

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
  $fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
?>

Any input would be appreciated. I'm pretty new to PHP and mySQL so it's a little rough for me. Thank you.

3
Contributors
2
Replies
9
Views
6 Years
Discussion Span
Last Post by sourcebits
0

Your variable names shouldn't be a part of your strings.

Example: $sql = "SELECT name, type, size, content FROM upload WHERE id=$id";

needs to be: $sql = "SELECT name, type, size, content FROM upload WHERE id=".$id;

0

If you will run this, the download dialog shows up. This will help.

<?php
$name = 'helo.jpg';
header("Content-type: image/jpeg");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.