TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Adding Images to a database from a folder (http://www.talkphp.com/advanced-php-programming/1948-adding-images-database-folder.html)

Rendair 01-13-2008 04:55 PM

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 :-D. There are many things you can check for including the file type as this only stops certain files, but it can be added to.

Wildhoney 01-13-2008 06:31 PM

Glob would look much neater than readdir :-( ! I personally think they should remove all the others and leave us with glob.

Rendair 01-13-2008 06:53 PM

Thank you Wild I shall take that into account :-D i agree with you.

RobertK 01-13-2008 07:40 PM

Quote:

Originally Posted by Wildhoney (Post 8379)
Glob would look much neater than readdir :-( ! I personally think they should remove all the others and leave us with glob.

I disagree; unless they add recursive search abilities to glob(), it's much easier to design with readdir() and scandir() than glob. You're making it out to be annoying, when it really isn't. Just don't use it if you hate it.

I wrote a tutorial seven months ago that you probably read, if you visit Pixel2Life and read tutorials there, about implementing recursive searches--and helped someone extend it to include keyword searching as well. It wouldn't be hard, at all, to check for an extension on the end of that. For one directory, sure glob is nice, otherwise forget it. I used scandir to get all elements within the directory, and then discern what I wanted to do with them from there.


All times are GMT. The time now is 07:43 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0