I have the below code in my script and I would like to add a check to this code so that when a user tries to upload an image, the system will check that the image name is unique from all other image names in the database. If there is an image with the same filename in the database an error msg will show up telling the user to change the image name or something.
Code: Select all
<?
session_start();
require('config/connection.php');
if (isset($_POST['btnCancel']))
{
header('Location: index.php');
}
else if (isset($_POST['btnRegister']))
{
$username = $_POST['txtUsername'];
$password = ($_POST['txtPassword']);
if (strlen(trim($username)) > 0 && strlen(trim($password)) > 0)
{
$confirmpassword = ($_POST['txtConfirmPassword']);
$firstname = $_POST['txtFirstName'];
$lastname = $_POST['txtLastName'];
$email = $_POST['txtEmail'];
if (strlen($_POST['txtDOB']) > 0)
{
$dob = explode("/", $_POST['txtDOB']);
$day = $dob[0];
$month = $dob[1];
$year = $dob[2];
$dob = date("Y-m-d", mktime(0,0,0,$month,$day,$year));
}
else
{
$dob = "";
}
$location = $_POST['txtLocation'];
$image = $_FILES['txtImage'];
$filename = "";
//checking if an image was uploaded
if ($image)
{
//checking if image is JPG
if ($image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg")
{
$filename = $image['name'];
//uploading the file
move_uploaded_file($image['tmp_name'], "images/users/" . $image['name']);
}
else
{
$message = "Only .jpg images are allowed to be uploaded";
}
}
if (isset($_POST['rbnGender']))
{
$gender = $_POST['rbnGender'];
}
else
{
$gender = "";
}
if ($password == $confirmpassword)
{
$insert = "INSERT INTO users (username, password, firstname, lastname, email, dob, location, gender, filename, userlevel) VALUES ('" . addslashes($username) . "', '" .
addslashes($password) . "', '" . addslashes($firstname) . "', '" . addslashes($lastname) . "', '" . addslashes($email) . "', '" . addslashes($dob) . "', '" . addslashes($location) . "', '" . addslashes($gender) . "', '" . addslashes($filename) . "', '0')";
mysql_query($insert) or die(mysql_error());
header('Location: index.php');
}
else
{
$message = "Error: <b>Passwords</b> do not match";
}
}
else
{
$message = "Error: <b>Username</b> and <b>Password</b> are mandatory";
}
}
?>