TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-13-2008, 04:55 PM   #1 (permalink)
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)
Old 01-13-2008, 06:31 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Glob would look much neater than readdir ! I personally think they should remove all the others and leave us with glob.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Rendair (01-13-2008)
Old 01-13-2008, 06:53 PM   #3 (permalink)
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

Thank you Wild I shall take that into account i agree with you.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 01-13-2008, 07:40 PM   #4 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Arrow

Quote:
Originally Posted by Wildhoney View Post
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.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:21 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design