KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Insert Image files into mysql by using Forms

Before you start this lesson, I assume that you finished reading the following tutorials:

  1. install phpmyAdmin, mysql , php
  2. Create Database by using phpMyAdmin
  3. Working with forms
  4. Insert Form Data into Database
  5. Retrieve data from Database
  6. Insert binary file into MySQL using phpMyAdmin
  7. Insert binary file into MySQL using PHP code

This lesson will show you how to insert binary files, such as PDF files, image files, MP3 files... etc into MySQL database using HTML form.

Assume you hosts online image storage business, your clients need load their images to your database. You may using the following steps to achieve this goal.

Create HTML form

Clients may use this form to submit their images, you might want to create a form like the following:

insert blob mysql,insert image mysql

<form action="upload.php" method="post" enctype="multipart/form-data">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<!-- Name of input element determines name in $_FILES array -->
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" id="upload" value="Upload binary file">
</form>

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. There is a configurable parameter in php.ini to limit upload file size, such as upload_max_filesize = 2M for PHP5.

Also note that your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.

Deal with Form and insert binary file into MySQL

Once clients locate their binary file and click on "Upload binary file" button, the Form's information will be transfered to the files dealing with form input:"upload.php".

There are 3 basic steps dealing with form input and insert binary file into MySQL:

  1. Get client files
  2. open database connection
  3. insert client binary file into database

Here is the code for "upload.php"

<?php
if(isset($_POST['upload']))
{
//get file information --step1
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];

//get file content -- step1
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
fclose($fp);

//connect to database --step2
$username="username"; //your user name
$password="yourpasword"; //input your password here.
$database="yourdatabase"; //your database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//insert into database --step3
$query = "INSERT INTO gallery (image) VALUES ('$content')";
mysql_query($query) or die('Error, query failed');

//return insert information to client
$id= mysql_insert_id();
echo "File<b> $fileName</b> uploaded as id= $id<br>";
}
?>

$_FILES['userfile']['size'] returns the size, in bytes, of the uploaded file.
$_FILES['userfile']['name'] returns t he original name of the file on the client machine.
$_FILES['userfile']['tmp_name'] returns the temporary filename of the file in which the uploaded file was stored on the server.

by default, client files will be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini.upload_tmp_dir by default is empty in php.ini(PHP5).

If you have read the lesson: Insert binary file using PHP code, you should be familiar with the left steps: get file content, connecting to database and insert binary file to MySQL.

$id= mysql_insert_id(); returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query.

The last line just show insertion information to client: like

File veryCuteImage.jpg uploaded as id=36

There are other alternative ways to insert binary files into MySQL:

  1. Using php programming insert binary files into MySQL
  2. Using HTML Form and php progamming insert binary files into MySQL(local and remote)
  3. Using PhpMyAdmin insert binary files.
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer