Professional Web Applications Themes

Downloading MySQL Files - PHP Development

I have a database which keeps some normal files in LONGBLOBS. I then call up a file called download.php to retrieve the file from the database, echo out itís contents, and itís type, and all that so they download it. However, if the user clicks the download link, they download the actual download.php file and if they open it, itís blank. Iíve attached the code to download.php. Any ideas why this is happening and how to fix it? <?php if (isset($HTTP_GET_VARS['id'])) { include('Connections/default.php'); mysql_select_db($database_default, $default); $id = $HTTP_GET_VARS['id']; $query = "SELECT * FROM files WHERE id='$id'"; $result = mysql_query($query, $default); ...

  1. #1

    Default Downloading MySQL Files

    I have a database which keeps some normal files in LONGBLOBS. I then call up
    a file called download.php to retrieve the file from the database, echo out
    itís contents, and itís type, and all that so they download it. However, if
    the user clicks the download link, they download the actual download.php
    file and if they open it, itís blank. Iíve attached the code to
    download.php. Any ideas why this is happening and how to fix it?



    <?php

    if (isset($HTTP_GET_VARS['id'])) {

    include('Connections/default.php');

    mysql_select_db($database_default,
    $default);

    $id = $HTTP_GET_VARS['id'];



    $query = "SELECT * FROM files WHERE
    id='$id'";

    $result = mysql_query($query, $default);

    $row = mysql_fetch_array($result);

    $new = $row['downloads'] + 1;

    $query = "UPDATE files SET downloads='$new'
    WHERE id='$id'";

    $result = mysql_query($query, $default);



    $type = $row["ftype"];

    $name = $row["fname"];

    $size = $row["fsize"];

    $id = $row["id"];

    $data = urldecode($row["fdata"]);



    header("Content-type: $type");

    header("Content-length: $size");

    header("Content-Disposition: attachment;
    filename=$name");

    header("Content-Description: PHP Generated
    Data");



    echo $data;

    exit();

    }

    ?>





    Thanks,

    Stephen Craton

    HYPERLINK "http://www.melchior.us"http://www.melchior.us -- HYPERLINK
    "http://www.melchior.us/portfolio"http://www.melchior.us/portfolio






    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.532 / Virus Database: 326 - Release Date: 10/27/2003


    Stephen Guest

  2. #2

    Default Re: [PHP] Downloading MySQL Files

    On Monday 10 November 2003 08:07 pm, Stephen Craton
    wrote: 
     

    Here is where you begin to get confused. The output of
    your php page will always be called download.php, no matter
    what is in it. The users are not downloading the
    contents of your download.php file since that file is
    obviously not blank. Rather they are downloading the
    output generated by your download.php, which is
    nothing.
     

    Are you getting any PHP errors? (Check your log.)

    Are you getting any MySQL errors? (You should include
    an error check in your statements.)

    If no to the above, then break the program down into
    parts and output debug statements. E.g. echo that the
    if conditional is being executed, echo the contents of
    the first SQL statement, echo the variables of the
    output, etc.

    Hope this helps.

    --John

     
    John Guest

  3. #3

    Default RE: [PHP] Downloading MySQL Files

    Urldecode(..) may not be binary safe. I'm not sure exactly what type of
    content it is you are storing in the database for 'fdata', but it sounds
    like you're misusing urlencode/urldecode. In short,
    urlencode(..)/urldecode(..) are for encoding URLs and are not the
    appropriate way to escape binary data for insertion into the database.

    Instead, you should be using mysql_escape_string(...) to escape the binary
    data upon insertion. Then, depending on your PHP configuration, use
    stripslashes(..) to restore the scalar when you pull the data back from the
    database.

    e.g.

    Mysql_query('INSERT INTO table (fdata)
    VALUES('.mysql_escape_string($fdata).')');

    Then, when you select your data...

    ....
    $result = mysql_query($query, $default);
    $row = mysql_fetch_array($result);

    $row[fdata] = stripslashes($row[fdata]);


    Now, $row[fdata] contains the raw binary form of the file you wish to send.



    Regards,

    Erik Osterman
    http://osterman.com/



    -----Original Message-----
    From: Stephen Craton [mailto:us]
    Sent: Monday, November 10, 2003 5:07 PM
    To: PHP List
    Subject: [PHP] Downloading MySQL Files

    I have a database which keeps some normal files in LONGBLOBS. I then call up
    a file called download.php to retrieve the file from the database, echo out
    it's contents, and it's type, and all that so they download it. However, if
    the user clicks the download link, they download the actual download.php
    file and if they open it, it's blank. I've attached the code to
    download.php. Any ideas why this is happening and how to fix it?



    <?php

    if (isset($HTTP_GET_VARS['id'])) {

    include('Connections/default.php');

    mysql_select_db($database_default,
    $default);

    $id = $HTTP_GET_VARS['id'];



    $query = "SELECT * FROM files WHERE
    id='$id'";

    $result = mysql_query($query, $default);

    $row = mysql_fetch_array($result);

    $new = $row['downloads'] + 1;

    $query = "UPDATE files SET downloads='$new'
    WHERE id='$id'";

    $result = mysql_query($query, $default);



    $type = $row["ftype"];

    $name = $row["fname"];

    $size = $row["fsize"];

    $id = $row["id"];

    $data = urldecode($row["fdata"]);



    header("Content-type: $type");

    header("Content-length: $size");

    header("Content-Disposition: attachment;
    filename=$name");

    header("Content-Description: PHP Generated
    Data");



    echo $data;

    exit();

    }

    ?>





    Thanks,

    Stephen Craton

    HYPERLINK "http://www.melchior.us"http://www.melchior.us -- HYPERLINK
    "http://www.melchior.us/portfolio"http://www.melchior.us/portfolio






    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.532 / Virus Database: 326 - Release Date: 10/27/2003

    Erik Guest

Similar Threads

  1. Downloading the right Mysql for mac os x 10.4.5
    By mistainfa in forum Coldfusion Database Access
    Replies: 3
    Last Post: March 24th, 08:28 PM
  2. Downloading files
    By Kimmers1125 in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: October 26th, 04:45 AM
  3. downloading file from mysql server
    By Asit van der Heijden in forum PHP Development
    Replies: 4
    Last Post: July 29th, 11:05 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139