Insert Image
files into mysql by using Forms
Before you start this lesson, I assume that you finished reading
the following tutorials:
- install phpmyAdmin, mysql
, php
- Create Database by using phpMyAdmin
- Working with forms
- Insert Form Data into Database
- Retrieve data from
Database
- Insert binary file into
MySQL using phpMyAdmin
- 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:

<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:
- Get client files
- open database connection
- 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:
- Using php
programming insert binary files into MySQL
- Using
HTML Form and php progamming insert binary files into MySQL(local
and remote)
- Using PhpMyAdmin
insert binary files.
|