|
||||||
| PHP Tutorials PHP Tutorials |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
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:
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: ****** Now create the database: Code:
mysql> CREATE DATABASE binary; Code:
Query OK, 1 row affected (0.00 sec) Code:
mysql> CREATE TABLE tbl_images (
> id tinyint(3) unsigned NOT NULL auto_increment,
> image blob NOT NULL,
> PRIMARY KEY (id)
> );
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"> 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:
PHP Code:
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. |
| Sponsored Links |
|
|
|
|||||
|
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! |
|
|||
|
Quote:
thnks |
|
|||||
|
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! |
|
|||||
|
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! |
![]() |
| Currently Active Users Viewing This Thread: 3 (0 members and 3 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |