Images downloaded from Mysql blob field are blank

Discussion in 'PHP' started by lostnote, Nov 7, 2007.

  1. #1
    O.K., this is driving me nuts. The file uploads seem to be going into the database fine. The correct name, file type, and file size are displayed in the appropriate fields, but when I try to download the file from MySQL and display it, I only get a blank image. What am I doing wrong? Thanks in advance.

    Here's the scripts:
    Upload...
    <form method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
    <input name="userfile" type="file" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>

    <?php
    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];


    $file_handle = fopen($tmpName, 'rb') or die( "Can't open file!" );

    // Copy the binary file data to the filedata table in sequential rows each containing MAX_SQL bytes
    // Your table should have an index set to auto_increment
    // Store the file_id to identify the data fragments
    $data = mysql_escape_string(fread(fopen($_FILES['userfile']['tmp_name'], "rb"), $_FILES['userfile']['size']));

    $result = mysql_query("INSERT INTO worksamples (file_name, file_type, file_size, work_content) VALUES ('$fileName', '$fileType', '$fileSize', '$data')");

    $file_id = mysql_insert_id();

    echo "<br>File $fileName uploaded<br>";
    }
    ?>


    Download...
    <?php
    $result = mysql_query("SELECT file_name, file_size, file_type, work_content FROM worksamples");
    $row = mysql_fetch_row($result);
    $name = $row[0];
    $type = $row[2];
    $size = $row[1];
    $content = $row[3];
    header("Content-type: $type");
    header("Content-length: $size");
    echo $content;
    ?>
     
    lostnote, Nov 7, 2007 IP
  2. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    id say to check the blob field in phpmyadmin and make sure the upload is working right... if not make sure the file type is reading correctly as well
     
    bobb1589, Nov 7, 2007 IP
  3. lostnote

    lostnote Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for replying. The file type is correct and there appears to be data in the blob field (roughly equal in kbytes to the file size), so I think that is working fine as well. Don't know what it could be. I'm trying to execute this code from within Drupal, so maybe that is causing some problems. I don't know.
     
    lostnote, Nov 8, 2007 IP
  4. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try making a blank page that will test it without anything else on the page other than the script... if your putting that code in the page with anything else it will most likely not work.. you cant send out header information after the page has loaded... you can call the image through <img src="display.php"> if you want to use it in another page...
     
    bobb1589, Nov 8, 2007 IP
  5. CEHonline

    CEHonline Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    it doesnt insert binary. had the same problem this morning. solved it by using this:

    $((VARIABLE)) = addslashes($the content variable()); and insert it by the $((variable))

    my example:
    $img = addslashes($myimage->GetImageContents());
     
    CEHonline, Nov 9, 2007 IP