Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2012, 05:37 PM   PM User | #1
loopsnhoops
New Coder

 
Join Date: Aug 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
loopsnhoops is an unknown quantity at this point
Exclamation Downloading Files using PHP from a DB

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 Code:
<?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 is offline   Reply With Quote
Old 04-26-2012, 06:55 PM   PM User | #2
loopsnhoops
New Coder

 
Join Date: Aug 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
loopsnhoops is an unknown quantity at this point
bumping
loopsnhoops is offline   Reply With Quote
Old 04-26-2012, 07:29 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by loopsnhoops View Post
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 Code:
<?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!
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 04-27-2012, 05:50 AM   PM User | #4
loopsnhoops
New Coder

 
Join Date: Aug 2011
Posts: 24
Thanks: 6
Thanked 0 Times in 0 Posts
loopsnhoops is an unknown quantity at this point
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.
loopsnhoops is offline   Reply With Quote
Old 04-27-2012, 01:00 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Tags
download, mysql, php, select, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 AM.


Advertisement
Log in to turn off these ads.