hey guys!
I have a script that should add images into the database. It does that. The only problem is that it does not only adds new images but also adds existing ones.
Im sure its a really simple task for you, for me its too much!
So again this code should check if an entry exists and then add the image if it does not exists.
Thank you 🙂
<?php
include('mysql.php');
if ($handle = opendir('images')) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if($file!='.' && $file!='..') {
$images[] = "('".$file."')";
}
}
closedir($handle);
}
$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
print mysql_error();
}
else {
print "finished installing your images!";
}
?>
You can make the
filenamefield unique (CREATE UNIQUE INDEX filename_idx ON images (filename)) then simply replaceINSERT INTO imageswithINSERT IGNORE INTO images.As an added bonus, you’ll now also have the
filenamefield indexed, which will speed upSELECTs byfilename.