Greetings All,

You helped be greatly last time I was here, hoping for a repeat.

I have a page at http://www.vfw10216.com/download_file.php that displays contents of my database that stores PDF files.

The contents field is type 'mediumblob' and attributes is 'binary'. The field does contain data in every record, as shown in the display results (only 5 records/files now).

Obviously I have the upload working. which is a form at http://www.vfw10216.com/upload_file.php

And, obviously the download page is finding and displaying the contents of the db field.

But, the links to the files don't work.

Here is my code so far. What do I need to do to it for the pdf files to actually download and open?

<?php

$query  = "SELECT name, type FROM upload";

$result = mysql_query($query);

$name = 'name';

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
  echo "<li> {$row['id']} Filename: <a href=download.php?id={$row['id']}> {$row['name']} </a> </li>";
}
?>

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

$id    = $_GET['id'];
$query = "SELECT name, type, size, content" .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename= $name");
echo $content;

exit;
}

?>

Recommended Answers

All 9 Replies

I do not see a mysql_connect/mysql_select_db, only a mysql_query.

I do not see a mysql_connect/mysql_select_db, only a mysql_query.

Yea, I didn't include my connect db section. I'm reposting the entire code with my connect parameters not included.

<?php

    // Connect to the database server
 $dbcnx = @mysql_connect("host name here", "username", "password");
  if (!$dbcnx) {
    echo( "<P>Unable to connect to the database server at this time.</P>" );
    exit();
  }

  if (! @mysql_select_db("db name here") ) {
    echo( "<P>Unable to locate the database.</P>" );
    exit();
  }

mysql_select_db("db name here", $dbcnx);
?>

<?php

$query  = "SELECT name, type FROM upload";

$result = mysql_query($query);

$name = 'name';

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
  echo "<li> {$row['id']} Filename: <a href=download.php?id={$row['id']}> {$row['name']} </a> </li>";
}
?>

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

$id    = $_GET['id'];
$query = "SELECT name, type, size, content" .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename= $name");
echo $content;

exit;
}

?>

Damn, missed it the first time:

$query  = "SELECT id, name, type FROM upload";

The id column is not in your query.

That displayed the id in the output, but it didn't help the link problem. Clicking the output link still gives me the 404 Error File Not Found

Perhaps because you also output the list of files, when an id is requested.

I have no idea. I'm new to this so there is a lot I just don't know yet.

What I see thus far is the page displays the file names by their id. running the mouse over each link shows this:

http://www.vfw10216.com/download.php?id=12

which tells me it is looking for and finding record #12. But when I click on its link it is trying to open that page as http://www.vfw10216.com/download.php?id=12

Is there a path statement missing or something?

I meant for you to do this:

<?php
  $dbcnx = @mysql_connect("", "", "") or die('<p>Unable to connect to the database server at this time.</p>');
  @mysql_select_db('') or die('<p>Unable to locate the database.</p>');

  if (isset($_GET['id']))
  {
    $id = $_GET['id'];
    $query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";

    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename= $name");
    echo $content;
  }
  else
  {
    $query  = "SELECT name, type FROM upload";
    $result = mysql_query($query);
    $name = 'name';
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
      echo "<li> {$row['id']} Filename: <a href=download.php?id={$row['id']}> {$row['name']} </a> </li>";
    }
  }
?>

Wow, that's great. It now does download and display contents of the file, but in the wrong format (jibberish). The download file is a pdf file, but is displaying like this:

%PDF-1.5 %���� 169 0 obj <> endobj 179 0 obj <<>/Filt

And I also get 3 warnings about the the 3 headers like this (1 warning for each header):

Warning: Cannot modify header information - headers already sent by (output started at /home6/patsworl/public_html/VFW10216/download.php:5) in /home6/patsworl/public_html/VFW10216/download.php on line 201

I'm thinking the Content-type needs to be application/pdf, which I tried, but with no luck.

Do I need to modify the headers to correct these 2 issues.

Hey, and thanks for your help and patience with an old man pritaeas.

I got it! I had to move the php code above the html code.

It now ass to open or save the file when clicking the lick. I need to figure out how to force it to open in an iframe on the same page.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.