Current location: Hot Scripts Forums » Programming Languages » PHP » How to upload image to sql table and resize it?


How to upload image to sql table and resize it?

Reply
  #1 (permalink)  
Old 01-02-09, 08:18 AM
tomming tomming is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
How to upload image to sql table and resize it?

Hello,I am here because I am very new about php.
First,I want to create a site to left people upload their photo,i want to know the basic how to upload photo to sql per user and resize it.I have a sql table which i get from website.


CREATE TABLE tbl_images (
id tinyint(3) unsigned NOT NULL auto_increment,
image blob NOT NULL,
PRIMARY KEY (id)
);

and dont forget to tell me how to retrieve image from the blob field
thank
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-02-09, 08:37 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
You should upload image to a folder and its name in database field.

Then you can easily retrieve image by name from database and Image from folder by name.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 01-02-09, 03:05 PM
groone groone is offline
New Member
 
Join Date: Jan 2009
Location: Mobile, ALabama
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
This code might help you get started. Blob uses too much space, convert to text


Code:
     if (empty($_FILES['imgfile'])){
          echo '<form action="form.php" method="post" enctype="multipart/form-data" >
               <input type="file" name="imgfile">
               <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
          </form>';
     }else{
          $image = $_FILES['imgfile'];
          //make sure this folder is writeable 666
          $path = '/home/user/public_html/images/';
          copy($image['tmp_name'], $path.$image['name']);

     //open file for reading
     $fp = fopen($path.$image['name'], "r");

          //measure the file contents
          $contents = fread($fp, filesize($path.$image['name']));

     //close the file
     fclose($fp);

     //encode the file to save space in the db
     $encoded = chunk_split(base64_encode($contents));

     //my assumption is that you already have a db setup with a longtext field.
     mysql_query("INSERT INTO images (ID, IMAGE) VALUES (" . $id . ",'" . $encoded . "')");

     //since it's inserted into mysql, you can delete the existing file
     unlink($path.$image['name']);
}
To get the image. Make sure there are no blank lines after the ?> or else it wont work

Code:
 <?php

     //My assumption is that you already have connected to the database somewhere
     //and the db has an id field
     $result=mysql_query("SELECT * FROM images WHERE ID='".$_GET['img']."'");

     //fetch data from database
     $data=mysql_fetch_array($result);

     $encoded=$data['data'];

     //close connection
     mysql_close($connection);

     //decode and echo the image data
     echo base64_decode($encoded);

?>

Last edited by groone; 01-02-09 at 03:07 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 01-02-09, 03:10 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by sandeep.kumar View Post
You should upload image to a folder and its name in database field.

Then you can easily retrieve image by name from database and Image from folder by name.
I agree, there is rarely any good reason to store the actual image in the database itself.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
auto table resize derick_2k JavaScript 4 04-26-04 03:32 PM
Fixing table cell height - dynamically resize column widths? ijg0 CSS 2 04-19-04 12:34 PM
An image stretcher script please. roofle Script Requests 0 03-09-04 03:41 PM


All times are GMT -5. The time now is 07:45 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.