Fetching image from MySQL database results in blank / invalid image file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyfka
    New Member
    • Jun 2007
    • 1

    Fetching image from MySQL database results in blank / invalid image file.

    Hello, I have a big problem. I have the following code but it just doesn't want to display my picture, it displays a small area without a picture, submitting and deleteng the image is working, but displaying...DO ESN'T :(

    //2.php
    [code=php]
    <?php

    // database connection
    $conn = mysql_connect(" localhost", "root", "triadpass" ) OR DIE (mysql_error()) ;
    @mysql_select_d b ("Books", $conn) OR DIE (mysql_error()) ;

    // Do this process if user has browse the file and click the submit button
    if ($_FILES) {
    $image_types = Array ("image/bmp",
    "image/jpeg",
    "image/pjpeg",
    "image/gif",
    "image/x-png");

    $userfile = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"])));
    $file_name = $_FILES["userfile"]["name"];
    $file_size = $_FILES["userfile"]["size"];
    $file_type = $_FILES["userfile"]["type"];

    if (in_array (strtolower ($file_type), $image_types)) {
    $sql = "INSERT INTO image (image_type, image_data, image_size, image_name, image_date) ";
    $sql.= "VALUES (";
    $sql.= "'{$file_type}' , '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
    @mysql_query ($sql, $conn);
    Header("Locatio n:".$_SERVER["PHP_SELF"]);
    exit();
    }
    }

    // Do this process of user has click a file name to view or remove
    if ($_GET) {
    $iid = $_GET["iid"];
    $act = $_GET["act"];
    switch ($act) {
    case rem:
    $sql = "DELETE FROM image WHERE image_id=$iid";
    @mysql_query ($sql, $conn);
    Header("Locatio n:./2.php");
    exit();
    break;
    default:
    echo "<img src=\"immage.ph p?iid=$iid\">";

    break;
    }
    }

    ?>
    <html>
    <head>
    <title>Storin g Images in DB</title>
    </head>
    <body>
    <form method="post" enctype="multip art/form-data">
    Select Image File: <input type="file" name="userfile" size="40"><inpu t type="submit" value="submit">
    </form>
    <?php
    $sql = "SELECT * FROM image ORDER BY image_date DESC";
    $result = mysql_query ($sql, $conn);
    if (mysql_num_rows ($result)>0) {
    while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
    $i++;
    $str .= $i.". ";
    $str .= "<a href=\"2.php?ii d=".$row["image_id"]."\">".$row["image_name "]."</a> ";
    $str .= "[".$row["image_date "]."] ";
    $str .= "[".$row["image_size "]."] ";
    $str .= "[<a href=\"2.php?ac t=rem&iid=".$ro w["image_id"]."\">Remove</a>]<br>";
    }
    print $str;
    }
    ?>
    </body>
    </html>
    [/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    //immage.php

    [code=php]
    <?php

    // database connection
    $conn = mysql_connect(" localhost", "root", "triadpass" ) OR DIE (mysql_error()) ;
    $mysql_select_d b ("Books", $conn) OR DIE (mysql_error()) ;
    $sql = "SELECT * FROM image WHERE image_id=".$_GE T["iid"];
    $result = mysql_query ($sql, $conn);
    if (mysql_num_rows ($result)>0) {
    $row = @mysql_fetch_ar ray ($result);
    $image_type = $row["image_type "];
    $image = $row["image_data "];
    Header ("Content-type: $image_type");
    print $image;
    }
    ?>[/code]
    Last edited by pbmods; Jun 17 '07, 11:35 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Split from original thread, merged duplicate posts.

    Comment

    Working...