insert image in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
goraya
Forum Newbie
Posts: 3
Joined: Fri Jun 12, 2009 5:46 am

insert image in database

Post by goraya »

hi me new in php
i have need help anyone pls
i need htm and php code for insert record and image in database table my sql
insert simple record i have done but image confusion me how i done it pls help me thnx
Dynex
Forum Commoner
Posts: 31
Joined: Mon Feb 09, 2009 7:24 pm

Re: insert image in database

Post by Dynex »

try this:

Code: Select all

if($_POST['submit'])
{
    if( $_FILES['file']['error'] > 0)
    {
        die("There was an error with the upload! Details: <br/>" . $_FILES['file']['error']);
    }
    
    $extension = $cms->get_file_type($_FILES['file']['name']);
    
    if($extension == "gif" || $extension == "jpg" || $extension == "jpeg" || $extension == "png")
    {
        $pass = true;
        if(preg_match('/^[.]/' , $_FILES['file']['name']) || preg_match('[.txt]', $_FILES['file']['name']) || preg_match('[.php]', $_FILES['file']['name']) || preg_match('[.html]', $_FILES['file']['name']) || preg_match('[.htm]', $_FILES['file']['name']) || preg_match('[.asp]', $_FILES['file']['name']) || preg_match('[.cm]', $_FILES['file']['name']) || preg_match('[.js]', $_FILES['file']['name']) || preg_match('[.xml]', $_FILES['file']['name']))
        {
            echo "<table class=error><tr><td>Hacking attempt!</td></tr>";
            include 'includes/footer.php';
            $pass = false;
            die();
        }
        if($pass == true)
        {
            if ($_FILES['file']['size'] <= 105000)
            {
                $name = $text->randomText(10);
                $name = $name . "." . $extension;
                $imageFolder = "images/profiles/{$name}";
                if ( move_uploaded_file($_FILES['file']['tmp_name'], $imageFolder) )
                {
                    list($width, $height, $type, $attr) = getimagesize("{$imageFolder}");
                    if($width <= 250 && $height <= 250)
                    {
                        $name = $db->escapeString($name);
                        $db->Query("UPDATE `playersSettings` SET `picture` = '$name' WHERE `id` = '$player_global[id]'");
                    }
                    else
                    {
                        echo "<table class=error bgcolor=#808080 align=center><tr><td class=text>Image deminsions are too big!</td></tr></table>";
                    }
                }
            }
            else
            {
                echo "<table class=content><tr><td class=text>Error uploading file!</td></tr></table>";
            }
        }
        else
        {
            echo "<tr><td>File not uploaded!</td></tr></table>";
        }
    }
    else
    {
        echo "<table class=content><tr><td class=text>Wrong image format!</td></tr></table>";
    }
}
?>
    <form method="POST" action="<?=$_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data">
    <table width="250" class="content" align="center" cellpadding="5" bgcolor="#808080">
        <tr>
            <td><img src="images/<?=$player_global['picture'] ?>"></td>
        </tr>
        <tr align="center">
            <td class="text">Max Dimensions: 250 x 250</td>
        </tr>
        <tr align="center">
            <td class="text">Max Size: 100k kb</td>
        </tr>
        <tr align="center">
            <td><input type="file" name="file" /></td>
        </tr>
        <tr align="center">
            <td><input class="submit" type="submit" name="submit" value="Upload!" /></td>
        </tr>
    </table>
    </form>
</td>
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Re: insert image in database

Post by shafiq2626 »

goraya wrote:hi me new in php
i have need help anyone pls
i need htm and php code for insert record and image in database table my sql
insert simple record i have done but image confusion me how i done it pls help me thnx
hi!
you would have to upload image in any folder and then save its path in a table field.
then when you detect value from table then give this value to the image src like this
<img src="images/<?php echo $value['image'];?>">

when you save date and upload file then rename that file like firstimage.jpg and then store it in image field of the table and then print it in the img tag.
Post Reply