Opening file stored in blob

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Opening file stored in blob

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Opening file stored in blob

Post 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'].
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Opening file stored in blob

Post 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.
Post Reply