tring to create a website where user can upload image.
i have 2 database
1-user(database)
1- which has user_id, email, passwod
2-image(database)
2-which has image_id, user_id, image(store image here), image_name ...etc

on upload.php page user has to fill this form hit sumbit. and it will run php code on upload.php

<form action="upload.php" method='post' enctype = 'multipart/form-data'>
    ImageName: <input class = "text" type="text" name="imagename" /><br/>
    Description: <input class = "text" type="text" name="description" /><br/>
    feedback<textarea cols="16" rows="4" nicewebsite> </textarea><br/>

    <input type ="file" name="fileupload" /><br/>
    <input type="submit" name="submit" value="sumbit" /><br/>
    <a href="index.php">[BACK]</a>
</form>

here i want to insert image to database. so for ex. if a user hwoarang(user_id 30) upload a image.
i want to store that image into table called image image_id(1), user_id(30), image(store image here)..etc

//store image in $image
            $image = file_get_contents($_FILES['fileupload']['tmp_name']);
            //store image name in $image_full_name(.jpeg)
            $image_full_name = $_FILES['fileupload']['name'];
            //store image size in $image_size
            $image_size = getimagesize($_FILES['fileupload']['tmp_name']);
            //test if its a image or file
            if($image_size == FALSE)
            {
                echo "Thats not a image";
            }
            else
            {
                //insert information into database
                if(!$insert = mysql_query("INSERT INTO VALUES(NULL, '', '$image', '$image_full_name', '', '', '', '')"))
                {
                    echo "problem uploading image";
                }
                else
                {
                    $lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p /><img src=get.php?image_id=$lastid>";
                }









full code=====================================================================================================
upload.php
<?php
session_start();
include("connect.php");

$user = $_SESSION['username'];
$file = $_FILES['fileupload']['tmp_name'];
$upload_error = "";

//user log in
if($user)
{
    if($file)
    {
        //user get submit button
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        { 
            //check for error first
            //scussess
            //store image in $image
            $image = file_get_contents($_FILES['fileupload']['tmp_name']);
            //store image name in $image_full_name(.jpeg)
            $image_full_name = $_FILES['fileupload']['name'];
            //store image size in $image_size
            $image_size = getimagesize($_FILES['fileupload']['tmp_name']);
            //test if its a image or file
            if($image_size == FALSE)
            {
                echo "Thats not a image";
            }
            else
            {
                //insert information into database
                if(!$insert = mysql_query("INSERT INTO VALUES(NULL, '', '$image', '$image_full_name', '', '', '', '')"))
                {
                    echo "problem uploading image";
                }
                else
                {
                    $lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p /><img src=get.php?image_id=$lastid>";
                }
            }
        }
    }
    else
    {
        echo "Please select an image.";
    }
}
else
{
    die("plz log in!");
}
?>

                                   <!-- enctype for upload images-->
<form action="upload.php" method='post' enctype = 'multipart/form-data'>
    ImageName: <input class = "text" type="text" name="imagename" /><br/>
    Description: <input class = "text" type="text" name="description" /><br/>
    feedback<textarea cols="16" rows="4" nicewebsite> </textarea><br/>

    <input type ="file" name="fileupload" /><br/>
    <input type="submit" name="submit" value="sumbit" /><br/>
    <a href="index.php">[BACK]</a>
</form>


get.php=====================================================================================================
<?php 
include("connect.php");

$user_id = $_REQUEST['user_id']; 

$image = mysql_query("SELECT * FROM image WHERE user_id = $user_id"); 
$image = mysql_fetch_assoc($image); //get access to image table
$image = $image['image'];

header("Content-type: image/jpeg");
?>

Recommended Answers

All 5 Replies

The first thing that stands out is :

if(!$insert = mysql_query("INSERT INTO VALUES(NULL, '', '$image', '$image_full_name', '', '', '', '')"))

INSERT INTO ... what, you've forgotten the table name.

Should be INSERT INTO tbl_name VALUES...

Also, consider using some other syntax to keep it a little cleaner (in my eyes), get rid of the empty values, e.g.

INSERT INTO tbl_name (User_ID, Image_File, Name) VALUES (23, 'TMP.JPG', 'Monkey');

That way you only insert what you need to, but make sure you have defaults set in your table denfinitions or things unexpected may happen.

from experience id recommened not storing the files in the db
store the url and just save th image to a predefined directory

that said to store files in a db you need to
upload file
open the file
read the contents
then save th contents to a blob type field
//
TINYBLOB
BLOB
MEDIUMBLOB
LONGBLOB
//

This type of topic has already been asked and answered gazilion times already. Please take a look at my sample implementation Here. Pretty much the same as what you are trying to achieve, included is a thumb generator with proportioning function in it..

Feel free to adjust the script to your needs....

it print out statment "problem uploading image"
.

if($_SERVER['REQUEST_METHOD'] == 'POST')
        { 
            $image_short_name =  $_POST['imagename'];
            $image_des =  $_POST['imagedes'];

            //check for error first
            //scussess
            //store image in $image
            $image = file_get_contents($_FILES['fileupload']['tmp_name']);
            //store image name in $image_full_name(.jpeg)
            $image_full_name = $_FILES['fileupload']['name'];
            //store image size in $image_size
            $image_size = getimagesize($_FILES['fileupload']['tmp_name']);
            //test if its a image or file
            if($image_size == FALSE)
            {
                echo "Thats not a image";
            }
            else
            {
                //get user id and sote in user_id_db
                $queryget = mysql_query("SELECT user_id FROM user WHERE username = '$user'") or die("query didnt work");
                $row = mysql_fetch_assoc($queryget);    
                $user_id_db = $row['user_id'];

                //insert information into database
                if(!$insert = mysql_query("INSERT INTO image VALUES(NULL, 'user_id_db', '$image', '$image_full_name', '$image_short_name', 'image_des')"))
                {
                    echo "problem uploading image";
                }
                else
                {
                    $lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p /><img src=get.php?image_id=$lastid>";
                }

Visit this lik
Click Here
that link contains on how you upload image from the server and it moves to a certain folder
what your goin to do is you will only store the path of the image to the database to minimize the space
and also use the stord path to to retrieve the image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.