Page 1 of 1

Opening file stored in blob

Posted: Thu Oct 14, 2010 12:57 pm
by marnieg
I have two different websites that both have a php frontend and mysql database. Both have a page where the user can upload a document which is stored in a table. The content of the file is stored in a blob field. Here is the database table definition.

CREATE TABLE filetable (
ID int(10) NOT NULL auto_increment,
filedata mediumblob NOT NULL,
filedescription varchar(50) default NULL,
filename varchar(50) NOT NULL default '',
filesize varchar(50) NOT NULL default '',
filetype varchar(50) NOT NULL default '',
username varchar(45) NOT NULL default '',
PRIMARY KEY (ID)
) TYPE=MyISAM AUTO_INCREMENT=131 ;

I am able to upload a pdf or word or xls file to the page using some php code for file uploading. It is the same on both websites.
Now for my issue. I have a page where the files from the database are displayed and provide link for the user to open the file. The issue is that the code works on one site but not the other. The only difference I can tell you is the one that works is running php 4.0 and the one that doesn't work is running 5.0

Is there a change in the way 5.0 is executing this code. It is identical on both

Code: Select all

<?php
if ($id) {
  include ("dbconnect.inc.php");
  $sql = "SELECT * FROM filetable WHERE ID=$id";

  $result = mysql_query($sql);
  $result = mysql_fetch_object($result);
  $username = $_COOKIE["user"];

      header("Content-type: $result->filetype");
      header("Content-length: $result->filesize");
      header("Content-Disposition: attachment; filename=$result->filename");
      header("Content-Description: PHP Generated Data");
      echo $result->filedata;
  }
?>
This is the code executed when they click on the "view or download" link.

Re: Opening file stored in blob

Posted: Thu Oct 14, 2010 1:01 pm
by AbraCadaver
Most likely register_globals is on in 4 and off in 5. If $id is from the URL then you need to use $_GET['id'].

Re: Opening file stored in blob

Posted: Thu Oct 14, 2010 1:25 pm
by marnieg
I tried changing the code to this

Code: Select all

if ($_GET['id']) {
  $id = $_GET['id'];
  $sql = "SELECT * FROM filetable WHERE ID=$id";
And still have the same problem. From the link on the previous page I see the URL(http://enersolllc.com/download.php?id=102)
for the record and it is correct.

I'm not getting the dialog box to choose Open or Save, it just opens the page with crazy characters.