[advanced search]
22 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 04-11-2005, 12:24 PM   · #1
squeaky14
New Member
 
Trader Rating: (0)
Join Date: Mar 2005
Posts: 9
NP$: 12.00 (Donate)
squeaky14 is an unknown quantity at this point
Talking Upload Image to Mysql Database with PHP

Hi, this is my first tutorial. I have tested it and it all works great.
It should work with any type of image.

Today, err.. class, were going to make an image >> database uploader.

Requirements:
-Basic knowledge of php and mysql.
-Access to a mysql database.

First of all, create a table (and a database if necessary):

CREATE TABLE `images` (
`img` INT NOT NULL AUTO_INCREMENT ,
`data` LONGTEXT NOT NULL ,
PRIMARY KEY ( `img` )
);

Secondly, create a directory called "temporary" and chmod it to 777.

Ok, now that you have made the table and directory lets get to the coding.
This script will consist of 2 pages, "upload.php" and "image.php".

First page, "upload.php":

PHP Code:
<?php
//connect to database. Username and password need to be changed
mysql_connect("localhost", "username", "password");

//Select database, database_name needs to be changed
mysql_select_db("database_name");

if (!
$_POST['uploaded']){
//If nothing has been uploaded display the form
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post"
ENCTYPE="multipart/form-data">
Upload:<br><br>
<input type="file" name="image"><br><br>
<input type="hidden" name="uploaded" value="1">
<input type="submit" value="Upload">
</form>

<?
}else{
//if the form hasn't been submitted then:

//from here onwards, we are copying the file to the directory you made earlier, so it can then be moved
//into the database. The image is named after the persons IP address until it gets moved into the database

//get users IP
$ip=$REMOTE_ADDR;

//don't continue if an image hasn't been uploaded
if (!empty($image)){

//copy the image to directory
copy($image, "./temporary/".$ip."");

//open the copied image, ready to encode into text to go into the database
$filename1 = "./temporary/".$REMOTE_ADDR;
$fp1 = fopen($filename1, "r");

//record the image contents into a variable
$contents1 = fread($fp1, filesize($filename1));

//close the file
fclose($fp1);

//encode the image into text
$encoded = chunk_split(base64_encode($contents1));

//insert information into the database
mysql_query("INSERT INTO images (img,data)"."VALUES ('NULL', '$encoded')");

//delete the temporary file we made
unlink($filename1);
}

//end
}
?>



Now thats the upload script complete, how do we view the images?

Second Page, "image.php":

PHP Code:
<?php
//connect to database. Username and password need to be changed
$connection=mysql_connect("localhost", "username", "password");

//Select database, database_name needs to be changed
mysql_select_db("database_name");

//get decoded image data from database
$result=mysql_query("SELECT * FROM images WHERE img='".$_GET['img']."'");

//fetch data from database
$data=mysql_fetch_array($result);
$encoded=$data['data'];

//note: "$data['data']" is the row "data" in the table we made.
//The image ID would be "$data['img']" for example


//close connection
mysql_close($connection);

//decode and echo the image data
echo base64_decode($encoded);

//end
?>



Now, test it. To view the images you upload go to:
http://www.yourdomain.com/image.php?img=1
http://www.yourdomain.com/image.php?img=2
http://www.yourdomain.com/image.php?img=3

and so on...

My first tutorial, tell me what you think. Did you find it hard to understand any parts?


Please register or log-in into NamePros to hide ads

Last edited by squeaky14 : 04-11-2005 at 12:42 PM. Reason: Missed out a few things :P
squeaky14 is offline   Reply With Quote
Old 04-12-2005, 09:20 PM   · #2
alegend
PHPHelpSite.com
 
Name: Sean
Location: Boston
Trader Rating: (108)
Join Date: Feb 2005
Posts: 2,580
NP$: 25.41 (Donate)
alegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud ofalegend has much to be proud of
nice tutorial, I might try it later.. Thanks!
alegend is offline   Reply With Quote
Old 04-12-2005, 09:26 PM   · #3
redhippo
Senior Member©
 
redhippo's Avatar
 
Name: Dan
Location: ANForum
Trader Rating: (73)
Join Date: Oct 2003
Posts: 5,245
NP$: 3629.30 (Donate)
redhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to beholdredhippo is a splendid one to behold
great tutorial...whens it gonna be a full script
__________________
|www.vb-skins.com|- Quality Premade Vbulletin skins for Your VB forum makeover |www.Banjir.com|-Free image hosting?For Sale!|AlphaScripts|Money Generating Scripts
[B]FREE VB Skins
redhippo is offline   Reply With Quote
Old 04-14-2005, 10:23 AM   · #4
PolurNET
₪NamePros Elite™
 
PolurNET's Avatar
 
Name: Anand | Starting Law School!
Location: Montreal, Canada ... likely moving to Halifax soon!
Trader Rating: (100)
Join Date: Jul 2004
Posts: 2,957
NP$: 783.30 (Donate)
PolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant futurePolurNET has a brilliant future
Member of the Month
January 2007 Alzheimer's Cancer Survivorship Third World Education Autism Save a Life Cystic Fibrosis Parkinson's Disease Lou Gehrig's Disease (ALS) Autism Multiple Sclerosis SIDS
excellent work! I'll try it soon
__________________
PolurNET Communications Avoid the Freeze, Enjoy the Breeze!
= A very special day, February 12th =
Proudly #1 on NamePros and WebHostingJury for over 3 years!
||LawPoint.org •• GREAT UK HOSTING DEALS NOW AT: ExcellentHost.com ••
PolurNET is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HOWTO: Install the Apache Web Server, Perl, PHP, and MySQL on Windows deadserious Webmaster Tutorials 96 05-27-2007 01:24 PM
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 09:09 AM
Tutorial: How to Install Apache2 MySQL and PHP on Windows deadserious Webmaster Tutorials 35 09-21-2005 09:46 PM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 01:17 PM

Site Sponsors
http://www.mobisitetrader.com/ Custom Logo Design
Advertise your business at NamePros
All times are GMT -7. The time now is 04:43 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0