download issue

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Locked
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

download issue

Post by sulen »

I need to know the mistakes with this code. It is supposed to download the required file based on id number but instead of downloading the file in the database, it gives the user the option to download download.php which is the file in which the script is present in and not the required file from the DB


<html>
<head><title>Download Page</title></head>
</html>

<?php
//connecting to the database
@ $db = mysql_pconnect("localhost", "root", "dbstuff3r");

//connection error

if(!$db)

{
echo "error could not connect to the database. please try again later";
exit;
}

//selecting database

mysql_select_db("extranet");
if ($id)
{
$query = "select file_data,name,file_type,file_size,downloads from files where id=$id";
$result = mysql_query($sql);
$data = @mysql_result($result, 0, "file_data");
$name = @mysql_result($result, 0, "name");
$size = @mysql_result($result, 0, "file_size");
$type = @mysql_result($result, 0, "file_type");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
readfile($name);
echo $data;
die;
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Code I gave you in your other thread, will post it here too and then look the topic - please use only one thread per topic - even if you revise the code:
viewtopic.php?t=12410

Code: Select all

<html>
<head><title>Download Page</title></head>
</html>

<?php
// you can add the connection error handling to the end of the statement:
@$db = mysql_connect('localhost', 'root', 'dbstuff3r') 
		or die('Error: could not connect to the database. Please try again later');

// selecting database
mysql_select_db('extranet');
if (!empty($_GET['id']) && is_numeric($_GET['id'])) {
   
   // I like to ensure that the number is an integer.
   $id = (int)$_GET['id'];
   
   // if `id` is a number then you don't need to put quotes around it.
   $query = "SELECT file_data, id, name, file_type, file_size, downloads FROM files WHERE id=$id";

   @$result = mysql_query($query);

   // it's a good idea to check that a result was returned before attempting
   // to use the data.
   if (mysql_num_rows($result) == 1) {
      // if you only want the associative array then you can use
      // mysql_fetch_assoc() instead of mysql_fetch_array().
      $row = mysql_fetch_assoc($result);

      $type      = $row['file_type'];
      $name      = $row['name'];
      $size      = $row['file_size'];
      $id        = $row['id'];
      $data      = urldecode($row['file_data']);
      $downloads = $row['downloads'];

      header('Content-type: '.$type);
      header('Content-length: '.$size);
      header('Content-Disposition: attachment; filename='.$name);
      header('Content-Description: PHP Generated Data');
      
      echo $data;
      exit;
   } else {
      echo 'That download is not available. Please try again.';
   }
}
?>
Mac
Locked