Image filename check

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Image filename check

Post 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";
}
}
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Image filename check

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Image filename check

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