Showing image from database

Discussion in 'PHP' started by bradenkeith, Aug 13, 2008.

  1. #1
    I have an image uploader to MySQL. I have it set up so that they can go to: address.com/content/images?id=1 and it will download image 1.

    How do I set it up so that it will display the image inside the browser instead of downloading it? I want to display it within a page so it needs to be <img src>

    here's the code

    
    <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="2000000">
    <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)
    {
    $author   = $info['edits'];
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    
    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);
    
    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }
    $query = "INSERT INTO upload (author, name, size, type, content ) ".
    "VALUES ('$author', '$fileName', '$fileSize', '$fileType', '$content')";
    
    mysql_query($query) or die('Error, query failed');
    
    echo "<br>File $fileName uploaded<br>";
    }
    ?>
    <br /><br />
    
    
    
    
    <?php
    if(isset($_GET['id']))
    {
    // if id is set then get the file with the id from database
    
    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
             "FROM upload WHERE id = '$id'";
    
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) =                                  mysql_fetch_array($result);
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
    
    exit;
    }
    
    ?>
     <?php
    	$username = $_COOKIE['ID_my_site']; 
    	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
    	$info = mysql_fetch_array( $check );
    	$author = $info['edits'];
    $query = "SELECT id, name FROM upload WHERE author='$author'";
    $result = mysql_query($query) or die('Error, query failed');
    if(mysql_num_rows($result) == 0)
    {
    echo "You currently have no images uploaded<br />";
    }
    else
    {
    while(list($id, $name) = mysql_fetch_array($result))
    {
    
    echo "<img src=main.php?id=$result[id] alt=\"".
                    htmlspecialchars($result[type].".")."\">"
    				?>
    <a href="main.php?id=<?php echo $id;?>"><?php echo $name;?></a> <br>
    <?php
    }
    }
    ?>
    
    
    <?php
    if(isset($_GET['id']))
    {
    // if id is set then get the file with the id from database
    
    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
             "FROM upload WHERE id = '$id'";
    
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) =                                  mysql_fetch_array($result);
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
    
    exit;
    }
    
    ?>
    
    
    PHP:
    My sad attempt is in there.

    Any help is appreciated.
     
    bradenkeith, Aug 13, 2008 IP
  2. FlakDevelopment

    FlakDevelopment Banned

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    remove this thing

    header("Content-Disposition: attachment; filename=$name");
     
    FlakDevelopment, Aug 13, 2008 IP