...

View Full Version : Downloading Files using PHP from a DB



loopsnhoops
04-26-2012, 05:37 PM
Hi,

I've made a large sum of posts recently and if I can figure out this problem with your help then you can say goodbye to my relentless pestering. I've made an upload script that lets user upload files to my db. The fields used in the table in the db are: id,name,size,type,file and respectively correspond to int,varchar,int,varchar,mediumBLOB. I will post the download code below so you guys can take a look at it and tell me what you think the solution is. The problem is that when I print out the data on the screen using echo all i get for the file is some directory which doesn't allow for downloading.


<?php

$Server="xxxx";
$User="xxxx";
$Password="xxxx";
$Database="xxxx";

$con = mysql_connect($Server,$User,$Password);

if(!$con){

die("Couldn't Connect " . mysql_error());

}

mysql_select_db($Database,$con);

$sql = "SELECT * FROM IB2FILES";

$ctq = mysql_query($sql,$con);
if (!$ctq)
{
die("SQL Error! Query is $query<br />Error is ".mysql_error());
}

while ($row = mysql_fetch_assoc($ctq)) {
echo "<table> <tr> <td>";
echo "ID: ";
echo $row['id'];
echo "</td> </tr> <tr> <td>";
echo "Name: ";
echo $row['name'];
echo "</td> </tr> <tr> <td>";
echo "Type: ";
echo $row['type'];
echo "</td> </tr> <tr> <td>";
echo "Size: ";
echo $row['size'];
echo " bytes";
echo "</td> </tr> <tr> <td>";
echo "tmp_file: ";
echo $row['file'];
echo "</td> </tr> <tr> <td>";
echo "</table>";
}


?>


This is an example of what I get:

ID: 4
Name: About Stacks.pdf
Type: application/pdf
Size: 466028 bytes
tmp_file: /usr/local/pem/vhosts/113282/tmp/phpltvObD


Thanks, Eric

loopsnhoops
04-26-2012, 06:55 PM
bumping

tangoforce
04-26-2012, 07:29 PM
The fields used in the table in the db are: id,name,size,type,file and respectively correspond to int,varchar,int,varchar,mediumBLOB.
...
The problem is that when I print out the data on the screen using echo all i get for the file is some directory which doesn't allow for downloading.


<?php
while ($row = mysql_fetch_assoc($ctq)) {
echo "<table> <tr> <td>";

echo "tmp_file: ";
echo $row['file'];
echo "</td> </tr> <tr> <td>";
echo "</table>";
}


?>


This is an example of what I get:
tmp_file: /usr/local/pem/vhosts/113282/tmp/phpltvObD


Thanks, Eric

You're storing the file inside the DB which means that you can't print a file on the webpage.

The only time you can print a file out is by itself (with appropriate headers to tell the browser that an attachment is coming). Trying to print a file into a webpage will end miserbly.

To print out file data you must not have any other output that would cause the headers to be sent such as html or even whitespace. You clearly show a html table being started.

Oddly though you say you're storing the file in a DB, you even use a blob to store it yet your print out is the temporary file name which should be stored as text in the DB!

loopsnhoops
04-27-2012, 05:50 AM
Thanks for the reply! Can you point me to any tutorials describing some of the stuff you are talking about because I am new to php/mysql and I don't understand some of it.

tangoforce
04-27-2012, 01:00 PM
Two biggies for you:

1) Download flashget and use it to download some files off the internet. Look at how the headers work.

2) Read the tip in my signature about headers already sent.

From both of those you should start to see something that looks familiar. File stream data is sent AFTER the headers however it must have its own UNIQUE headers - it can't share the headers with the html page. Secondly you can't print a file into the html page. Supposing you're offering a mp3 of a .avi file - You can't just print it into the html page and expect the browser to understand it. The browser will just see a bunch of random characters and try to display it as a web page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum