Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 34

Thread: Blob

  1. #1
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts

    Blob

    How do I take an image from the database and insert it into the website, when I do this the image comes up in loads of letters and symbols.

    Thanks,
    Josh

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Please post your current code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts
    It's the image div line.

    Code:
    <?php require_once("includes/connection.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    <?php include("includes/header.php"); ?>
    <div id="content">
    		<?php
    		// 3. Perform database query
    		$result = mysql_query("SELECT * FROM pages", $connection);
    		if (!$result) {
    			die("Database query failed: " . mysql_error());
    		}
    	
    		// 5. Use returned data
    		while ($row = mysql_fetch_array($result)) { 
    	?> 
    		<div class="menu-name"> <?php echo $row["menu_name"]." <br />"; ?> </div>
    		<div class="buying-text"> <?php echo $row["content"]." <br />"; ?> </div>
    		<div class="image"> <?php echo $row["image"]." <br />"; ?> </div>
    		
    		<?php
    			}
    	?>
    
    	
    	
    </div>
    <?php require("includes/footer.php"); ?>
    You might also be able to tell me how to write it better, but it worked so that's how I left it. (I'm a real amateur with PHP)

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    You can't echo an image like this. You need to use <img> tag having the src as another file, which fetches the image based on the reference passed from original file.
    Take a look at http://www.phpbuilder.com/board/show...91&postcount=3
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    i think you can you file_get_contents and put that data in the db with an id and then use a seperate php page lets say

    <img src="imageloader.php?id=1" />

    then load that image dynmicaly and echo the data out with an image header() based on its extention.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Id have to write it first before i comment further... :-)
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    j05hr (11-01-2009)

  • #7
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts
    That would be really helpful Phil, thanks

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    aha! done it.. let me clean it up..
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    okay, made this for if you were storing image from off your server(the net elsewhere, will change to suit if needed).


    index.php

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php
    if(isset($_POST['imageInsert']))
    {
        function 
    checkUrl($url)
        {
            
    $ch curl_init();    
            
    curl_setopt($chCURLOPT_URL,            $url);
            
    curl_setopt($chCURLOPT_HEADER,         true);
            
    curl_setopt($chCURLOPT_NOBODY,         true);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($chCURLOPT_TIMEOUT,        10);
            
    $header curl_exec($ch);

            if(
    preg_match("#\s200\sOK#is"$header))
            {
                return 
    TRUE;
            }
            else
            {
                return 
    FALSE;    
            }
        }
        
        
    $con mysql_connect("localhost""root""");
        
    $db mysql_select_db("image-store"$con);
        
        if(
    checkUrl($_POST['url']))
        {
            
    $image mysql_real_escape_string(file_get_contents($_POST['url']));
            if(
    mysql_query("INSERT INTO `images` (image) VALUES ('$image') ") or die (mysql_error()))
            {
                echo 
    "insert complete";
            }
            else
            {
                echo 
    "could not insert error occured";    
            }
        }
        else
        {
            echo 
    "url incorrect";    
        }
    }
    elseif(isset(
    $_POST['imageShow']))
    {
        
    $src "imageLoader.php?id=".$_POST['id'];
        echo 
    "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
        echo 
    "<img src=\"".$src."\" />\n";
    }
    else
    {
        echo 
    "<h2>No image Selected To Show</h2>\n";    
    }
    ?>

    <h2>Upload Image</h2>
    <form method="post" action="index.php">
        url: <input type="text" name="url" />
        <input type="submit" name="imageInsert" />
    </form>

    <h2>Show image</h2>
    <form method="post" action="index.php">
        id: <input type="text" name="id" />
        <input type="submit" name="imageShow" />
    </form>


    </body>
    </html>
    imageLoader:

    PHP Code:
    <?php

    if(isset($_GET['id']))
    {
        
    $con mysql_connect("localhost""root""");
        
    $db mysql_select_db("image-store"$con);
        
    $id $_GET['id'];
        
        
    $get_query mysql_query("SELECT * FROM `images` WHERE id = '$id'") or die(mysql_error());
        
    $get_array mysql_fetch_array($get_query);
        
    $image_contents $get_array['image'];
        
        
    header('Content-Type: image/gif');
        echo 
    $image_contents;
    }

    ?>

    PROBLEMS:

    I have only took inter consideration .gif images. can do it for jpeg, gif and png (will do it all in a min)

    For the script above to work, created a table called "image-store" with fields 'id' (autocred, primary) and 'image'.

    For it to work this all image types you will have to add another field called, lets say 'ext' and i will sort that out.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    NEW for all images:

    php on index page:

    PHP Code:
    <?php
    if(isset($_POST['imageInsert']))
    {
        function 
    checkUrl($url)
        {
            
    $ch curl_init();    
            
    curl_setopt($chCURLOPT_URL,            $url);
            
    curl_setopt($chCURLOPT_HEADER,         true);
            
    curl_setopt($chCURLOPT_NOBODY,         true);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($chCURLOPT_TIMEOUT,        10);
            
    $header curl_exec($ch);

            if(
    preg_match("#\s200\sOK#is"$header))
            {
                return 
    TRUE;
            }
            else
            {
                return 
    FALSE;    
            }
        }
        
        
    $con mysql_connect("localhost""root""");
        
    $db mysql_select_db("image-store"$con);
        
    $url $_POST['url'];

        if(
    preg_match("#((\.jpeg)|(\.jpg)|(\.gif)|(\.png))$#is"$url$match))
        {
            if(
    $match[1]==".jpg")
            {
                
    $ext ".jpeg";
            }
            else
            {
                
    $ext trim($match[1]);
            }
            
    $ext substr($ext1);
            if(
    checkUrl($_POST['url']))
            {
                
                
    $image mysql_real_escape_string(file_get_contents($_POST['url']));
                if(
    mysql_query("INSERT INTO `images` (image, ext) VALUES ('$image', '$ext') ") or die (mysql_error()))
                {
                    echo 
    "insert complete";
                }
                else
                {
                    echo 
    "could not insert error occured";    
                }
            }
            else
            {
                echo 
    "url incorrect";    
            }
        }
        else
        {
            echo 
    "invalid file type";    
        }
    }
    elseif(isset(
    $_POST['imageShow']))
    {
        
    $src "imageLoader.php?id=".$_POST['id'];
        echo 
    "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
        echo 
    "<img src=\"".$src."\" />\n";
    }
    else
    {
        echo 
    "<h2>No image Selected To Show</h2>\n";    
    }
    ?>
    imageLoader.php

    PHP Code:
    <?php

    if(isset($_GET['id']))
    {
        
    $con mysql_connect("localhost""root""");
        
    $db mysql_select_db("image-store"$con);
        
    $id $_GET['id'];
        
        
    $get_query mysql_query("SELECT * FROM `images` WHERE id = '$id'") or die(mysql_error());
        
    $get_array mysql_fetch_array($get_query);
        
    $image_contents $get_array['image'];
        
    $image_ext $get_array['ext'];
        
        
    header("Content-Type: image/$image_ext");
        echo 
    $image_contents;
    }

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #11
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts
    It's getting there, it uploads the image but it doesn't show the actual image, do I have to add another field to my database called ext?

    Thanks for all your help so far!

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    yeah,

    id = init
    image = longblob
    ext = text

    the above code works bangon!...if i do say so myself...
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #13
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Phil Jackson View Post
    yeah,

    id = init
    image = longblob
    ext = text

    the above code works bangon!...if i do say so myself...
    It's not working on mine, I got all 3 of the fields, when I upload an image it show's the .gif icon but not the actual picture and when I put in a gif url, I get this error,

    Fatal error: Call to undefined function curl_init() in C:\wamp\www\image\index.php on line 15

    On this line of code,

    $ch = curl_init();

  • #14
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    you, ve not got Curl enabled.... wait there
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #15
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    change the function with the curl in to:

    PHP Code:
    function checkUrl($url)
        {
            if(@
    file_get_contents($url))
            {
                return 
    TRUE;
            }
            else
            {
                return 
    FALSE;    
            }
        } 
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •