We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,916 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to upload image

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");
?>
5
Contributors
5
Replies
6 Days
Discussion Span
11 Months Ago
Last Updated
6
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

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.

Smeagel13
Junior Poster
104 posts since Oct 2011
Reputation Points: 15
Solved Threads: 10
Skill Endorsements: 7

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
//

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

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....

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

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>";
                }
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

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.

code739
Posting Whiz in Training
208 posts since May 2012
Reputation Points: 17
Solved Threads: 28
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3094 seconds using 2.76MB