NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Webmaster Tutorials
Reload this Page Upload Image to Mysql Database with PHP

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 04-11-2005, 01:24 PM THREAD STARTER               #1 (permalink)
New Member
Join Date: Mar 2005
Posts: 9
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
????: NamePros.com http://www.namepros.com/webmaster-tutorials/81935-upload-image-to-mysql-database-php.html
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($fp1filesize($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
????: NamePros.com http://www.namepros.com/showthread.php?t=81935
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?
Last edited by squeaky14; 04-11-2005 at 01:42 PM. Reason: Missed out a few things :P
squeaky14 is offline  
Old 04-12-2005, 10:20 PM   #2 (permalink)
PHPHelpSite.com
Join Date: Feb 2005
Location: Boston
Posts: 2,580
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  
Old 04-12-2005, 10:26 PM   #3 (permalink)
NamePros Expert
 
redhippo's Avatar
Join Date: Oct 2003
Location: ANForum
Posts: 5,218
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?[B]For Sale!|AlphaScripts|Money Generating Scripts
FREE VB Skins
redhippo is offline  
Old 04-14-2005, 11:23 AM   #4 (permalink)
Senior Member
 
PolurNET's Avatar
Join Date: Jul 2004
Location: I ♥ Business Law
Posts: 3,082
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 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


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 02:24 PM
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 10:09 AM
Tutorial: How to Install Apache2 MySQL and PHP on Windows deadserious Webmaster Tutorials 35 09-21-2005 10:46 PM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 02:17 PM

Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:39 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger