Hi, I had a user management system which give me a problem. If a user change his profile photo in different format, that will duplicated. I use a naming convention in which the photo is named according to the user's username.
Sample:
unknown has unknown.jpg photo, if he changes his photo and choose a .gif file, unknown.jpg can't be removed and unknown.gif will be added in the photo folder. now, current photo of unknown is unknown.gif
Here's the code that accept the action once submitted:
<?php
session_start();
header ("Cache-control: private");
if (!$_SESSION ['Username' ])
{
echo "You're not logged in!";
require_once("login.php");
exit();
}
$username =$_SESSION ['Username'];
include("config.php");
$maxfilesize =81920 ;
// Check if there was a file uploaded
if (!is_uploaded_file($_FILES ['userphoto' ][ 'tmp_name' ]))
{
$error ="you didn't select a photo to upload.<br />" ;
}
else
{
if($_FILES ['userphoto' ][ 'size' ] > $maxfilesize)
{
$error ="your image file was too large.<br />" ;
unlink($_FILES ['userphoto' ][ 'tmp_name' ]);
}
else
{
$ext =strrchr($_FILES ['userphoto' ][ 'name' ], "." );
if ( $ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".bmp" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG" AND $ext != ".BMP" )
{
$error ="your file was an unacceptable type.<br />" ;
unlink ($_FILES ['userphoto' ][ 'tmp_name' ]);
}
else
{
if ($_SESSION ['Picture'] != "nopic.gif" )
{
unlink ("/UserPix/" .$_SESSION ['Picture']);
}
$newname =$_SESSION ['Username'].$ext;
move_uploaded_file($_FILES ['userphoto' ][ 'tmp_name' ], "UserPix/" .$newname);
mysql_query("UPDATE staff SET Picture='$newname' WHERE Username='$username'" ) or die ( mysql_error ());
$_SESSION ['Picture' ] = $newname ;
}
}
}
?>
<html>
<head>
<title>
</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if ($error)
{
echo "<font style='font-family:Tahoma; color:#000000; font-size:11px'>Your photo could not be changed because " .$error ."<br><a href='javascript:history.back()'>Go Back</a></font>" ;
}
else
{
echo "<script>window.close();</script>";
}
?>
</body>
</html>