Skip to main content
Inspiring
October 31, 2011
Answered

Duplicate File names

  • October 31, 2011
  • 1 reply
  • 701 views

Hi all,

Please find script below.

I'm trying to stop duplicate filenames. So if there is a file named the same as the one trying to uploaded already in the database it flags up as an error and doesn't process the upload script.

I thought the below would work but does not appear to at all, the photo upoads fine and data is posted to database but if I purposefully try to upload a file I lknow already exists, it overwrites the existing file.

Any thoughts?

Tom

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO modern (imageID, imageName, imageTitle, imageDescription) VALUES (%s, %s, %s, %s)",

                       GetSQLValueString($_POST['imageID'], "int"),

                       GetSQLValueString($_FILES['imageName']['name'], "text"),

                       GetSQLValueString($_POST['imageTitle'], "text"),

                       GetSQLValueString($_POST['imageDescription'], "text"));

   mysql_select_db($database_connJohnnash, $connJohnnash);

  $Result1 = mysql_query($insertSQL, $connJohnnash);

  if (!$Result1 && mysql_errno() == 1062) {

            $error['imageName'] = '<strong>' . $_FILES['imageName']['name'] . '.jpg </strong>is already in use and connot be overwritten. Please rename the image and try again.';

  } elseif (mysql_error()) {

            $error['dbError'] = 'Sorry, there was a problem with the databse. Please try again later.';

  } else {

            //Image Upload

//Set the maximum upload size in bytes

$max = 250000;


          //define the path to upload folder

          $destination = '../modern-images/';

          require_once('../Ps2/Upload.php');

          try {

                    $upload = new Ps2_Upload($destination);

                    $upload->move();

                    $result = $upload->getMessages();

          } catch (Exception $e) {

          echo $e->getMessage();

         

}

//End Image Upload - start processing the rest of the form

  $insertGoTo = "view-modern.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

}

Message was edited by: sellador315

This topic has been closed for replies.
Correct answer bregent

What is the primary key of the 'modern' table? Does 'imageName' have a unique index?

1 reply

bregentCorrect answer
Participating Frequently
November 1, 2011

What is the primary key of the 'modern' table? Does 'imageName' have a unique index?

Inspiring
November 1, 2011

I did give 'imageName' a unique Index, however, your post reminded me that there was already duplicate file names in the database when a gave it a unique index and that I never went back to check it worked!

So sorry to waste time basically!

Participating Frequently
November 2, 2011

No problem, not a waste of time.