Honk-honk!
I'm gonna embark on a similar quest soon, so let's check this out.
Don't know what data you store about your pic's, but here's how I would (try to) do it:
<?PHP
## Set headers to prevent double exec of this script! ##
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
## Set file-dir ##
$graphic_dir = "/home/knutm/public_html/includes/images";
## Get images from db ##
$getGraphics = mysql_query("select * from picture_table");
## Loop for picture details ##
while($sRow = mysql_fetch_array($getGraphics)) {
## Find some info ##
$pic_name = strtolower($sRow[picture_name]);
if(substr($pic_name,-4) == ".jpg")) {
$fType = ".jpg";
} else if (substr($pic_name,-4) == ".gif")) {
$fType = ".gif";
} else if (substr($pic_name,-4) == ".png")) {
$fType = ".png";
} else {
## you may want to do something here if file is not web-compatible ##
}
## Extract only filename, not ending ##
$sfName = explode(".", $sRow[picture_name]);
## Pile new filename ##
$fName = $sRow[picture_id] . "_" . $sfName[0] . $fType;
## Check for duplicates ##
if(!file_exists("$graphic_dir/$fName") {
$chk = "OK";
$fdbname = $fName;
## Open and write file ##
$fp = fopen("$graphic_dir/$fName", "w");
$fw = fwrite($fp, $sRow[file]);
fclose($fp);
} else {
## Duplicate exists - change filename to 10chr random hex ##
$randName = substr(md5(uniqid("Yes,wehavenobananas!!!!")), 0, 10);
$fdName = $sRow[picture_id] . "_" . $randName . $fType;
$chk = "Duplicate - filename changed";
$fdbname = $fdName;
## Open and write file ##
$fp = fopen("$graphic_dir/$fdName", "w");
$fw = fwrite($fp, $sRow[file]);
fclose($fp);
}
## Do update of new db_field ##
$regPic = mysql_query("insert into new_picture_table values ('$sRow[picture_id]', '$graphic_dir', '$fdbname', '$sRow[width]', '$sRow[height]', '$sRow[picture_category]')");
if($regPic) {
## echo status for row ##
echo $fdbname . "Status: " . $chk;
} else {
echo "Some unfathomable error has ocurred. Please visit phpbuilder again!";
}
}
?>
If you do a refresh after running this, it should refuse and give a warning. If it doesn't, it'll overwrite all previously written files with random char names 😉
Should probably be tested on a small, non-production db first...
knutm