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 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Problem pulling file from mySQL field

    When I download a file using this script, it always ends up the right size, but somehow corrupted, however, the commented out code below it works and saves a file from the SQL field to a file local to the server, and thus proves that the file stored in the mySQL db is fine. I'm not sure what is happening here....any help would be much appreciated.

    PS - I addslashes when I store the file in the bin_data field, but if I stripslashes when I pull it out, neither code sample works.

    <?php
    $LoanID = 'Black01';
    if ($LoanID) {
    include "Connect.php";

    $sql = "SELECT bin_data, filetype, filename, filesize FROM attachments WHERE LoanID='$LoanID'";

    $result = @mysql_query($sql);
    $data = @mysql_result($result, 0, "bin_data");
    $name = @mysql_result($result, 0, "filename");
    $size = @mysql_result($result, 0, "filesize");
    $type = @mysql_result($result, 0, "filetype");

    header("Content-type: $type");
    header("Content-length: $size");
    header("Content-Disposition: attachment; filename=$name");
    header("Content-Description: PHP Generated Data");
    echo $data;

    /*
    $thefile = "testDL.zip";
    $fp = fopen($thefile,"w");
    fwrite($fp,$data);
    fclose($fp);
    */
    }
    ?>
    Jason B

  • #2
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    update

    Well, the DB code has nothing to do with this,

    as replacing

    $data = @mysql_result($result, 0, "bin_data");

    with

    $data = fread(fopen("testDL.zip", "rb"), filesize("testDL.zip"));

    yields the same result, a corrupted file on download....I don't get it, I must be screwing something up.....when I send the data, because the HTML headers work, I don't see how I can mess up echo $data though...
    Jason B

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why is the file stored inside the db? Can't you just store it in your filesystem and onlys store the fileadress in the db ?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure what the problem is exactly, but I do have a cool script for uploading files, listing the files in the db, and allowing for download.

    This may not be of much use to you depending on how your stuff is setup, because it may in fact require you to redo everything.. but that might be okay too?

    I can't remember where i got it from, but i've attached the files nonetheless. I've also included the database schema required. Feel free to play with it as you like.

    Hope that helps,
    Sadiq.
    Attached Files Attached Files

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    raf et al,

    Just wondering the reasons for and against storing files in the db and on the file system.

    I suppose it depends on the application too. I've only had one experience, where I've got an information system in which there are many client records. Each of these records will have files associated with them (in some cases 20+ files) all with different properties and meanings. In this sort of scenario I prefer to store the files in the database as the only time you would view this file is through the client record while in the system.

    Also, naming the files is something my clients do not want to deal with. I guess it's easy enough to give a unique name to these files...

    Anyway, just wondering..

    Sadiq.

  • #6
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    raf
    I'm storing the files in a database becuase they are small <300K each, and there will never be more than 20 or so stored at any one time. Also, so I can secure them a little better without having to have the admin screw with .htaccess. The auth system I wrote only works on php files, so I don't really want to have any other(customer) files local.

    I've debugged to the point that I've determined the database storage/retrieval code is fine, but there is a bug in the way I am trying to download the data with Content-Disposition header I think.....according to php manual there is an IE bug that sometimes causes problem with this.

    Anyways, sad69, I appreciate the thought, but the download.php script in your example is missing from the zipfile. the listing.php shows links to download.php I would appreciate if you could send me a copy of that download.php so I can compare it to my own.....

    Thanks,
    Jason B

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh man, I'm sorry about that.. it should be part of the new attachment.

    Sadiq.
    Attached Files Attached Files

  • #8
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I'm officially at a loss

    Well, I'm officially at a loss, my code basically does the same thing as the example code posted. It's not a problem with the Content-Disposition header, or I simply wouldn't get prompted to download the file.....

    could VERY possibly be a problem in my code somewhere, but I can't seem to see one, after MUCH debugging, hoping maybe this is an apache win32 prob, but if so, someone else would've probably already known about it.

    After I set the headers, I've tried echo $, I've tried, readfile("testDL.zip") , both yield the same result, a correctly sized, but corrupt downloaded file.

    I've tried reading source file from DB, I've tried reading directly for source file on server - same results.

    WINDIFF shows that it apparently is adding a blank line at the beginning of every file that gets downloaded with this script....grrrhh.

    If anyone is bored, Here is the code, pretty lean and pruned, just download.php, upload.php, and listing.php. Also included file to create table with...

    Thanks again guys....
    Attached Files Attached Files
    Jason B

  • #9
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem Solved

    turns out I had one space after the closing ?> tag on my download page....removed space and all is well....

    thanks for the help guys...
    Last edited by black3842; 04-17-2004 at 01:04 AM.
    Jason B


  •  

    Posting Permissions

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