View Single Post
Old 01-13-2008, 04:55 PM   #1 (permalink)
Rendair
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default Adding Images to a database from a folder

Hey all

I am currently working on a new gallery script and i thought i would show my script on adding images to a database that are in a folder. This can be very helpful if you don't want to add them all one at a time.

First things first need to connect to the database. You can do this in your own way.

PHP Code:
 include("config.php");
 
connectTo(); //connect function 
Now to set up some variables.

PHP Code:
$path "../images/"// link to images
$counter 1//counter used later
$dh opendir($path); //open the image location 
Now we can loop through all the images in that folder and add them to the database.

first we want to stop other files from being added including the "." & ".." folders plus any files like index or something.

PHP Code:
while ($file readdir($dh))
 if(
$file != "." && $file != ".." && $file != "Thumbs.db" && $file != "index.html" && $file != "index.php"
 {
   {
   
   }
 } 
Now because we are adding to the database we want to get some information from the image. In this example the database base has the following fields.


Title - Title of the image
Date - Date is was submitted
About - Information about the image
Views - Number of views
Filename - The file name of the image "image.jpg"


Now because we are adding images via a folder we dont know anything about the image as in a description or even the actual title of the image the user wants, but this system allows for easy adding of images in a short time. So we can actually add some basic information for each image that the user can then change later.

PHP Code:
$title explode(".",$file);
$image_title $title[0];
$about "Please enter your discription here";
$date date("d, M Y"); 
Now one thing we want to do is actually check the database before we add to it. We do not want to add images to it that are already in it.

PHP Code:
//Check for any images already in database to not add ones we already have
$query "SELECT * FROM images WHERE filename='".$file."'";
$query mysql_query($query);
$number mysql_num_rows($query);
if(
$number 0){
   echo 
"The file <b>".$file."</b> is already in the database<\br>";
}else{
  
//image ain't in the database already so add it
  
$query "INSERT INTO images
  (
    title,
    date,
    about,
    views,
    filename 
  ) 
  VALUES
  (

    '
$image_title',
    '
$date',
    '
$about',
    '
$views',
    '
$file'
  )"
;

$query mysql_query($query) or die(mysql_error());

Of course we want to add something to tell the user we have added the image to the database and give the details for each one.

PHP Code:
if($query){
  echo 
$counter.". The file<b>".$file." </b>has been added to the database.....<br>";
  echo 
"<b>Image Details</b><\br>";
  echo 
"<b>Filename:</b> ".$file."<\br>";
  echo 
"<b>Image Title:</b> ".$image_title."<\br>";
  echo 
"<b>Image Discription:</b> ".$about."<\br>";
  echo 
"<\br><\br>";
                        
  
$counter++;    

Now there is one last thing we want to add and that is a timer kind of. We dont want to add each image straight after each other. This may cause it to crash. So we want to add something to give it a couple of seconds to recover then add the next one. We can use a for loop to do this.

PHP Code:
for($i=0;$i<5000;$i++){
                      

Now lastly we want to close the link with the directory

PHP Code:
closedir($dh); // close dir 
And there you go a nice way of adding images from a folder to a database . There are many things you can check for including the file type as this only stops certain files, but it can be added to.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
danielneri (01-13-2008)