Page 1 of 1

Image filename check

Posted: Thu Apr 15, 2010 3:50 pm
by tito85
Hi,

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 name in the database an error msg will show up telling the user to change the image name or something.

Any help please?

Code: Select all

//checking if an image was uploaded
if (strlen($image['name']) > 0)
{
//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']);
//remove old image
if (strlen($currentimage) > 0)
unlink("images/users/" . $currentimage);
}
else
{
$message = "Only .jpg images are allowed to be uploaded";
}
}

Re: Image filename check

Posted: Fri Apr 16, 2010 12:37 am
by flying_circus
I dont see anything in your code that relates to a database, but you can use the php function file_exists() to check if the file exists on the file system.

Re: Image filename check

Posted: Fri Apr 16, 2010 2:20 am
by tito85
My code is - Any Help please because I'm Stuck...

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"; 
} 
} 
?>