uploading an image file into mysql and viewing the image file

Discussion in 'Programming' started by contra10, Jan 4, 2009.

  1. #1
    i'm looking to solve this php trouble script it seems as if i can upload the data but i can't view it

    
    	<?php 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("registration") or die(mysql_error());
    	
    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'];
    
    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = mysql_real_escape_string($content);
    fclose($fp);
    
    if(!get_magic_quotes_gpc())
    {
        $fileName = mysql_real_escape_string($fileName);
    }
     
    
    $query = "INSERT INTO image (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
    
    mysql_query($query) or die('Error, query failed'); 
    
    echo "<br>File $fileName uploaded<br>";
    } 
     
    
    ?>
    </body> 
    </html>
    	
    <table border="1" cellpadding="10">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" 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>
    </form>
    </table>
    
    <?php
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("registration") or die(mysql_error());
    
    $query  = "SELECT * FROM image WHERE id= '1'";
    $result = mysql_query($query);
    
    $row = mysql_fetch_assoc($result);
    
    header("Content-type: image/gif");
    echo($row['content']);
    ?>
    [/code
    Code (markup):
     
    contra10, Jan 4, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    place error_reporting(E_ALL); on both files begining and see what it says
     
    crivion, Jan 5, 2009 IP