开发者

Check for entries before processing

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 开发者_C百科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 filename field unique (CREATE UNIQUE INDEX filename_idx ON images (filename)) then simply replace INSERT INTO images with INSERT IGNORE INTO images.

As an added bonus, you'll now also have the filename field indexed, which will speed up SELECTs by filename.


Let's see. To check if exiting filenames are already stored in the database you must do the following:

  • retrieve filenames from the database
  • check if current file is between them

PS: You should require your mysql.php file, without it, the rest of your script won't work.

<?php
require_once('mysql.php');

$existing_files = array();
$qry = mysql_query("SELECT filename FROM images WHERE 1");
while($row = mysql_fetch_assoc($qry)) {
    array_push($existing_files, $row['filename']);
}


if ($handle = opendir('images')) {
    while (false !== ($file = readdir($handle))) {
        if($file!='.' && $file!='..' && !in_array($file, $existing_files)) {
            $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 should add filename as unique key in your database.

Or, you could use this php script:

$exists = mysql_query("SELECT * FROM images 
   WHERE filename LIKE '%".mysql_real_escape_string($images[0])."%'");
if(mysql_num_rows($exists == 0)){
  $query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
  if (!mysql_query($query)) {
    print mysql_error();
  }
  else {
    print "finished installing your images!";
  }
}


Why not do an md5sum on the file, and use that for uniqueness, then do a select to see if the file is there, if it isn't insert if it it and return the id as the approve state, or if it already exists the id of the existing record. Doing it by filename is fine, unless, several people are uploading the same file but named differently. The downside to doing this is if the files are big, then it takes some of your resources to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜