Lost Password?


  #1 (permalink)  
Old 03-12-2008, 08:40 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,666
Last Blog:
PHP Objects, Patterns,...
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: Storing Images in MySQL with PHP

In this tutorial I will show you how to store binary images (or any binary file) in MySQL using PHP.

Why store binary files in MySQL using PHP?
You may want to allow users to upload files to your PHP script or store banner images from advertisers inside of your SQL database. Instead of creating and using a messy directory structure it may be more suitable to store them directly in your SQL database along with the advertiser or user information.

Reasons to store your images (binary files) in a database:
  1. Storing in a database allows better security for your files and images.
  2. Eliminates messy directory and file structures (this is especially true when users are allow to contribute files).
  3. Files/Images can be stored directly linking to user or advertiser information.
You may want to reconsider storing your binary files in the database. Here are some reason you may not want to:
  1. You can't directly access your files using standard applications such as FTP.
  2. If you database becomes corrupt, so do your files.
  3. Migrating to a new database is made more difficult
  4. Pulling images from a database and displaying them is slower than using file access.
Note: all of the banners and user attachments that CodeCall has are stored directly in MySQL.

Creating the Database and Table
Using the console we will access MySQL and create a database and table for our binary upload PHP script. Using a tool such as PHPMyAdmin can make this task a bit easier. If your webhost has cpanel or Plesk then you probably have access to PHPMyAdmin. If your host does not you might want to consider hosting from ToastedPenguin.com.

First login to MySQL:
Code:
# mysql -u root -p
   Enter Password: ******
Replace the user root with your username for MySQL. Since I'm running this on my local machine I will access the database using the root account via the console and PHP Script. This is not recommended and you should not access your database in a public environment, such as the internet, using the root user, ever!

Now create the database:
Code:
mysql> CREATE DATABASE binary;
Press Enter, you should see results similar to:
Code:
Query OK, 1 row affected (0.00 sec)
Lets create the table now:
Code:
mysql> CREATE TABLE tbl_images (
     > id tinyint(3) unsigned NOT NULL auto_increment,
     > image blob NOT NULL,
     > PRIMARY KEY (id)
     > );
What is a BLOB?
Above we created two tables, one the primary ID (of the row/entry) and the binary BLOB. A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.

Source


You've now created your database and are ready to upload binary files to it. In this case, images. Next we will create a simple upload script.


Creating the HTML
Creating the user interface where you or your visitors will upload files is very simple. Using basic HTML and relying on the browser to do most of the heavy lifting you can easily create an upload form. Here is the one we will use:

HTML Code:
<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
Copy this code into a file and save it as add.html

The code above allows the uses to browse for a file and select any image/jpeg file type. Once selected and the submit button is pressed the image contents are forwarded to PHP script, insert.php.


Inserting the Image into MySQL
Create a new file named insert.php. If you change the name of this file you will also need to change the action="" part in your HTML file above. Open the file in your favorite text editor or PHP IDE.

Write or copy/paste this code into your file:
PHP Code:
 // Create MySQL login values and 
// set them to your login information.
$username "YourUserName";
$password "YourPassword";
$host "localhost";
$database "binary";

// Make the connect to MySQL or die
// and display an error.
$link mysql_connect($host$username$password);
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}

// Select your database
mysql_select_db ($database); 
The above code sets your connection variables, creates a connection to MySQL and selects your database (binary if you followed my instructions above). Next we need to read the form data and insert it into the database.

PHP Code:
// Make sure the user actually 
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      
// Temporary file name stored on the server
      
$tmpName  $_FILES['image']['tmp_name'];  
       
      
// Read the file 
      
$fp      fopen($tmpName'r');
      
$data fread($fpfilesize($tmpName));
      
$data addslashes($data);
      
fclose($fp);
      

      
// Create the query and insert
      // into our database.
      
$query "INSERT INTO tbl_images ";
      
$query .= "(image) VALUES ('$data')";
      
$results mysql_query($query$link);
      
      
// Print results
      
print "Thank you, your file has been uploaded.";
      
}
else {
   print 
"No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?> 
The PHP code above takes the user selected image, reads it with fopen and stores the data in a variable named $data. The binary data is then inserted into our database for retrieval at a later time.

Save the file above and access add.html via your browser. Select an image file and upload it.

Questions and Attachments
If you have any questions please ask them here. I'll try to answer them when I can. I've attached working copies of the add.html and insert.php scripts to this thread. If the tutorial was useful please post a simple "thank you".

In the next PHP Tutorial I'll show you how to retrieve these binary images from MySQL and display them to your visitors.


Part II: By Jaan
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!

Last edited by Jordan; 04-29-2008 at 09:40 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-23-2008, 10:16 AM
schumid schumid is offline
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0
schumid is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP

thank you that really works
please post the code how to retrieve the images from the database
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-23-2008, 06:09 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,666
Last Blog:
PHP Objects, Patterns,...
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Tutorial: Storing Images in MySQL with PHP

I'll probably add the tutorial for showing images from MySQL this week. Keep an eye out.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-29-2008, 07:03 PM
schumid schumid is offline
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0
schumid is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP

Quote:
Originally Posted by Jordan View Post
I'll probably add the tutorial for showing images from MySQL this week. Keep an eye out.
hey jordan sorry for asking again, can you post the tutorial for showing images from mysql as soon as possible ?
thnks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 01:05 AM
Whitey's Avatar   
Whitey Whitey is offline
Programming Professional
 
Join Date: Feb 2008
Location: Loveland, Colorado
Posts: 225
Rep Power: 6
Whitey has a spectacular aura aboutWhitey has a spectacular aura aboutWhitey has a spectacular aura about
Send a message via AIM to Whitey Send a message via MSN to Whitey Send a message via Skype™ to Whitey
Default Re: Tutorial: Storing Images in MySQL with PHP

Yea please
I would really like to know as well.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-20-2008, 08:03 PM
xiou xiou is offline
Newbie
 
Join Date: Apr 2008
Posts: 4
Rep Power: 0
xiou is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP

Thanks so much for this tutorial. I am looking forward to the tutorial on retrieving the images from the database.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-29-2008, 09:39 AM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,666
Last Blog:
PHP Objects, Patterns,...
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Tutorial: Storing Images in MySQL with PHP

Jaan wrote the Part II of this tutorial. You can find it here: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-22-2008, 08:46 AM
quickfox902's Avatar   
quickfox902 quickfox902 is offline
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0
quickfox902 is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP

need help with creating sql tables in sql 5.0

Error

SQL query:

mysql > CREATE TABLE tbl_images(
> id tinyint( 3 ) unsigned NOT NULL AUTO_INCREMENT ,
> image blob NOT NULL ,
> PRIMARY KEY ( id ) >
)

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> CREATE TABLE tbl_images (
> id tinyint(3) unsigned NOT NULL auto_in' at line 1

can anyone help me?

Last edited by quickfox902; 06-22-2008 at 09:00 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-22-2008, 09:20 AM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,666
Last Blog:
PHP Objects, Patterns,...
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Tutorial: Storing Images in MySQL with PHP

Don't type "mysql>" into MySQL as part of your query.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 06-22-2008, 06:24 PM
quickfox902's Avatar   
quickfox902 quickfox902 is offline
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0
quickfox902 is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP

thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: PHP to MySQL Jordan PHP Tutorials 9 09-05-2008 01:12 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 04:05 PM


All times are GMT -5. The time now is 08:37 AM.