Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    bumping

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    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!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •