Trouble displaying binary content

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

  1. #1
    I'm new at this, so please forgive my ignorance. Any help would be greatly appreciated.

    Basically, the files and I'm uploading to the database from an HTML form are not displaying properly when viewed by a web browser. In the database, I can see that the names, file types, and file sizes are correct, and that there is content in the LONGBLOB field entitle, "work_content." However, every time I try to load one of these binary files into a web browser, I get thousands of strange characters instead of an image.

    I'm running PHP 5.2.3, Apache 2.2.4, and MySQL 5.0.45.

    Here's the code from the page containing the file upload form:

    <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>";
    }
    ?>

    And here's the code from the page which is trying to download the file:

    <?php
    $result = mysql_query("SELECT file_type, work_content FROM worksamples");
    $row = mysql_fetch_row($result);
    echo $row[1];
    ?>

    Again, any help on this would be greatly appreciated. And thanks for your time.
     
    lostnote, Nov 4, 2007 IP
  2. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You probably need to send a correct file type to the browser:

    
    <?php
    $result = mysql_query("SELECT file_type, work_content FROM worksamples");
    $row = mysql_fetch_row($result);
    header('Content-type: '.$row[0]);
    echo $row[1];
    ?>
    
    PHP:
     
    theOtherOne, Nov 4, 2007 IP
  3. lostnote

    lostnote Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for replying. Unfortunately, when I add the header line and try that, all I get is a blank webpage with the url printed in the upper right hand corner. Any thoughts?
     
    lostnote, Nov 4, 2007 IP