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

Image Hosting by PHP

Before you start this lesson, please read the following lesson first:

This lesson will show you how to create image hosting database by using PHP and MySQL

Create image hosting database

In this database we need store image's type(jpg,gif,tif....etc), image original name, image,and a unique id for stored image.

Suppose the image hosting table is named as imagehosting, it has 4 columns: id(auto increment), type(Varchar),name(Varchar) and image(MEDIUMBLOB).

The SQL command like the following

CREATE TABLE `imagehosting`
( id` INT NOT NULL AUTO_INCREMENT ,
`type` VARCHAR( 30 ) NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
`image` MEDIUMBLOB NOT NULL ,
PRIMARY KEY ( `id` ) ) TYPE = MYISAM ;

If you have read the lesson: Using phpMyAdmin Create Database and using phpMyAdmin insert binary file you should be able create this database in a snap.

Create HTML Form for uploading image

You clients need an interface to upload their images. The interface is the form you gonna create.

Using form insert binary file into MySQL already shows how to create the Form. Just use it.

Uploading image into database

The lesson Using form insert binary file into MySQL use a table with 2 columns. Here we use 4 columns in our image hosting table. So upload.php is changed like following:

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

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

//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
$link=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 (type,name,image) ". "VALUES('$fileType','$fileName','$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>";
echo "Retrieve URL=http://www.yourwebsite/download.php?id=$id";
mysql_close($link); //close connection
}
?>

get_magic_quotes_gpc is checking magic quotes set or not. If not set, addslashes($fileName); will add back slashes to the special charaters in $fileName like O\'Realy become O\\\'Realy, so that it could be stored in MySQL properly.

echo "Retrieve URL=http://www.yourwebsite/download.php?id=$id"; just send a reminder to your customers how to retrieve their images. You customer could use the following HTML to retrieve their image:

<img src="http://www.yourwebsite/download.php?id=16">

Retrieve Image from Database

download.php is similar to the one we used in lesson Retrieve binary file from MySQL.

<?php
//connect to database

$id=$_GET['id'];//get id
$username="yourusername";
$password="yourpassword"; //input your password here.
$database="yourdatabase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//query database
$query = "SELECT name,type,image FROM youtablename WHERE id = $id";

$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image"); //get content,

$type=mysql_result($result,0,"type"); //get type

$filename=mysql_result($result,0,"name"); //get file name

//send pdf to requesting page
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$filename");
echo $content;
mysql_close($link); //close database connection
?>

The only difference is we add header("Content-Disposition: attachment; filename=$filename"). You may add this line if you want the user be prompted to save the data you are sending, for example, you are sending PDF, zip, ...etc. Otherwise, you may delete this line.

Please not that header("Content-Disposition: attachment; filename=$filename") won't prompt user if customers load their image like

<img src="http://www.yourwebsite/download.php?id=16">

It will prompt user to save file if used like following

<a href ="http://www.yourwebsite/download.php?id=16"> myimage</a>

More applications

The above code is suitable for any binary files. You may use it for PDF file hosting, Software hosting(exe,zip files), music hosting(MP3,mid)...

Try this example.

 
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