I have code that states, if an image filename already exists, add a number to it. If that image is again upload, increment it yet again, so nothing gets overwritten, ie:
test.jpg
1_test.jpg
2_test.jpg
It's not working that way, what's really happening is:
1_test.jpg
2_1_test.jpg
At any rate, the first image gets uploaded. It increments the filename 1_test.jpg. When the next comes up, which would be 2_1_test.jpg, NO IMAGE GETS UPLOADED! (grr)
Here is my logic:
// ***************************************************************
// Do this if form is submitted...
// ***************************************************************
if ($HTTP_POST_VARS[submit]) { // Add the product
if (!$my_validator->validate_fields("name, description")) {
echo $my_validator->error;
echo "<BR><BR><a href=\"javascript:history.back()\">Go Back and Try Again</a>";
break;
} else {
// if $userfile isn't empty, then copy new image to server and thumbnail it
if("$f2" != "none") {
stripslashes($userfile);
copy($userfile,$graphic);
//this creates a thumbnail and copies it to thumbnail directory
system("convert -quality 90 -sample '$new_wx$new_h>' '$graphic' '$graphics'");
//*****************************************************************
//first check to see if an image already exists with the same name
//*****************************************************************
$sql_image = "SELECT image
FROM product_inventory";
$query_image = mysql_query ($sql_image);
//$query_row = mysql_fetch_array ($query_image);
$n = 1;
while ($query_row = mysql_fetch_array ($query_image)) {
if ($userfile_name == $query_row['image']) {
//echo "Filename exists. Created new filename.";
$userfile_name = $n++."_".$query_row['image'];
}
}
//check to see what format the graphic is in
if (stristr($userfile_name,".tif")) { //if TIF, convert to jpg
//change format of image to jpeg and resize to specs
system("mogrify -format jpg -sample '$new_hx$new_w<' '$dir$directory$userfile_name'");
copy($userfile,$graphic);
//get rid of the TIF file
unlink($dir.$directory.$userfile_name);
$userfile_name=str_replace(".tif",".jpg",$userfile_name);
} elseif (stristr($userfile_name,".bmp")) { //if bmp, convert to jpg
//change format of image to jpeg and resize to specs
system("mogrify -format jpg -sample '$new_hx$new_w<' '$dir$directory$userfile_name'");
copy($userfile,$graphic);
//get rid of the TIF file
unlink($dir.$directory.$userfile_name);
$userfile_name=str_replace(".bmp",".jpg",$userfile_name);
} elseif (stristr($userfile_name,".jpg")) { //if jpg, resize
copy($userfile,$graphic);
//resize to specs
system("mogrify -sample '$new_w<' '$dir$directory$userfile_name'");
} elseif (stristr($userfile_name,".gif")) { //if gif, resize
copy($userfile,$graphic);
//resize to specs
system("mogrify -sample '$new_w<' '$dir$directory$userfile_name'");
} elseif (!stristr($userfile_name,".gif") || !stristr($userfile_name,".tif") || !stristr($userfile_name,".bmp") || !stristr($userfile_name,".jpg") || !stristr($userfile_name,".jpeg")) { //if anything else except TIF, BMP, JPG or GIF, reject the image
echo "<B>This is not a TIF, JPG or GIF image. Please try again.</B>";
echo "<BR><BR><a href=\"javascript:history.back();\">Return</a>";
exit;
}
if (!is_uploaded_file ($userfile)) { //if not uploaded, spit back error
echo "<b>$userfile_name</b> couldn't be copied !!";
}
// check whether it has been uploaded
if (is_uploaded_file ($userfile)) { //if the image uploaded successfully, tell us that it was
echo "
<b>$userfile_name</b> copied succesfully !!";
}
} else {
stripslashes($userfile);
$userfile_name = "default.gif";
}
// ************************************************
// Add the products
// ************************************************
# Create the Query.
$sql = "INSERT INTO product_inventory(name, image, category, description)
VALUES('$name', '$userfile_name', '$category', '$description')";
# Make the Query
$ok = mysql_query ($sql);
if ( !$ok ) {
echo " <font color=\"red\">
Did not add record. Try again.</font><br><br>";
} else {
echo "<BR>Record added.<BR>";
}
}
}