insert image into database

Discussion in 'PHP' started by mirosoft1, Feb 6, 2008.

  1. #1
    i have a form to insert image and the title of image
    i make a table on the database

    CREATE TABLE `images` (
    `image_id` int(11) NOT NULL auto_increment,
    `image` longlob default NULL,
    `title` text default NULL,
    )

    but i donot know how to insert image into database
    please help
     
    mirosoft1, Feb 6, 2008 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You need to insert the data from a form?

    INSERT INTO images (image, title) VALUES ('imagelinkhere','title')
    Code (markup):
     
    HuggyStudios, Feb 6, 2008 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    but i put on the form file browse to allow user insert image from hard disk
     
    mirosoft1, Feb 6, 2008 IP
  4. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    this is the code of the form

    <body><form action="" method="post" enctype="multipart/form-data">
    <table width="200" border="1">
    <tr>
    <td>image title </td>
    <td><input type="text" name="textfield"></td>
    </tr>
    <tr>
    <td>image</td>
    <td><input type="file" name="file"></td>
    </tr>
    </table>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    </body>
     
    mirosoft1, Feb 6, 2008 IP
  5. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Store the image in the table as binary.

    Then retrieve it as such.
     
    LittleJonSupportSite, Feb 6, 2008 IP
  6. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    i change the table database `image` binary
    but how i can retrieve it
    can you give me the code of retrieval????
     
    mirosoft1, Feb 6, 2008 IP
  7. WebHostingNerds.com

    WebHostingNerds.com Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    save the image in a directory as a file and store it's name in the db
    each time you name the just uploaded image as max(id)+1.jpg (or gif, png etc)
    this is just the concept

    you need to send the form to a page, you can specify this in the form action, action="thefile.php", and you must use enctype="multipart/form-data"

    in thefile.php you must parse what you you get from the forum via the post method (you can't use GET), save and rename the file, run the insert on the db...

    the concept is easy, if you've never dealt with such stuff just start from somewhere and read tuts step by step for what you need...
     
    WebHostingNerds.com, Feb 6, 2008 IP
  8. bjplink

    bjplink Peon

    Messages:
    243
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'd avoid actually storing the image in the DB. Just store the name of the file in the DB (or give it a new name, whatever) and put the image in a directory somewhere after upload. Then just reference the image later on using the value you stored in the DB.
     
    bjplink, Feb 6, 2008 IP
  9. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #9
    I have to agree with the last two posts. Storing binary image information in a database just seems like a waste of resources to me. Why do you want the image in a data base instead of simply storing it in a directory?
     
    ToddMicheau, Feb 6, 2008 IP
  10. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #10
    When I was learning PHP, the author of the book I was reading made a comment about this. Unfortunately I can't find the exact quote right now but it was something like this...

    Databases are great for storing things, but if you ever find yourself wondering what's the best data type to hold an image - think again.

    I thought to myself, "When would you ever want to store an image in a database instead of a file?" But I guess it does happen...

    Gotta echo the other responses. Have your script save it in a centrally located directory (like /uploads/images) and then save the filename to the database. Much quicker/more efficient than grabbing the entire image data from the database each time.

    - Walkere
     
    walkere, Feb 6, 2008 IP
  11. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    In order to store files like images in a database you need to use the BLOB (Binary Large OBject) datatype. I'm with the others in saying that there are better ways to upload and reference images though, as even just a handful of images has the potential to choke up your database under moderate load. Unless you have a dedicated server and prefer to do things in ways that are unecessarily complicated and inefficient, just upload the file normally and use the database to point to its location.
     
    The Critic, Feb 6, 2008 IP
  12. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #12
    I'm also with everyone that has suggested not to store the image in the database. Use the table's primary key in the filename to make sure that you have a unique filename though. Saving the image in the file system will help backing up your site more manageable - storing images in the database can make the database grow very large.

    Brew
     
    Brewster, Feb 7, 2008 IP
  13. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #13
    ok but how i can save the image into a file?
     
    mirosoft1, Feb 7, 2008 IP
  14. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Below is the code I use to upload 5 image files to my server, and it automatically inserts the information to my database, I also have a script for viewing them.
    
    <?php
    $page_title = 'Upload a File';
    include ('includes/forms.html');
    $counter = 5;
    
    if (isset($_POST['submitted'])) {
    	require_once ('includes/mysql_connect.php');
    	
    	for ($i = 0; $i < $counter; $i++) {
    		
    		$path = '/uploads/';
    		$filename = 'upload' . $i;
    		$description = 'description' . $i;
    		$email = 'email' . $i;
    		
    		if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {
    		if (!empty($_POST[$email])) {
    			$e = "'" . escape_data($_POST[$email]) . "'";
    		} else {
    			$e = 'NULL';
    		}
    		
    		if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {
    		if (!empty($_POST[$description])) {
    			$d = "'" . escape_data($_POST[$description]) . "'";
    		} else {
    			$d = 'NULL';
    		}
    		
    		$query = "INSERT INTO uploads (email, file_path, file_name, file_size, file_type, description) VALUES ($e, $path, '{$_FILES[$filename]['name']}', '{$_FILES[$filename]['size']}', '{$_FILES[$filename]['type']}', $d)";
    		$result = mysql_query ($query);
    	
    	if ($result) {
    	
    		$upload_id = mysql_insert_id();
    		
    		if (move_uploaded_file($_FILES[$filename]['tmp_name'], "uploads/$upload_id")) {
    		echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>';
    	}  else {
    	
    		echo '<p>File number ' . ($i + 1) . ' could not be moved.</p>';
    		
    		$query = "DELETE FROM uploads WHERE upload_id = $upload_id";
    		$result = mysql_query ($query);
    	
    	}
    	
    } else {
    	echo '<p>Your submission could not be processed due to a system error.  We apologise for any inconvenience.</p>';
    }
    }
    }
    }
    mysql_close();
    }
    ?>
    <form enctype="multipart/form-data" action="add_file.php" method="post">
    <fieldset>
    <h3>Fill out the form to upload Your File(s):</h3>
    <input type="hidden" name="MAX_FILE_SIZE" value="524288" />
    <?php
    for ($i = 0; $i < $counter; $i++) {
    	echo '<p><b>Email Address:</b> <input type="text" name="email' . $i . '" size="40" maxlength="40" /></p>
    	<p><b>File:</b> <input type="file" name="upload' . $i . '" /></p>
    	<p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br />
    	';
    	}
    	?>
    	
    	</fieldset>
    	<input type="hidden" name="submitted" value="TRUE" />
    	<div align="center"><input type="submit" name="submit" value="Submit" /></div>
    
    </form>
    <?php
    include ('includes/foot.html');
    ?>
    
    PHP:
     
    Alley Cat, Feb 7, 2008 IP