Jump to content


Photo

Rename Uploaded file


  • This topic is locked This topic is locked
2 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 23 September 2006 - 03:51 PM

Hey guys,
I've changed my format from my last file uploading script
still uploading an avatar, but i've decided to store the
image inside a folder called "images" and I wanted to
rename the image.

My problem, I'm not sure how to rename an uploaded image
but i wanted it to be:
USERNAME_av.<EXT> example (ash_av.PNG, or joe_av.jif)
if you understand what i'm getting at, then i can add that to
the database, and it will be easier to show their avatar?

my code
function UploadAvatar(){
if(isset($_POST['upload']) && empty($_POST['upload']))
	{
	echo "file was left blank.";
	return false;
	}
else
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$max = "1048576 "; //1mb max size
		
		$Types = array('image/png',
		'image/jif',
		'image/jpg',
		'image/bmp');
		
		if(!in_array($fileType = $_FILES['userfile']['type'], $Types))
			{
			echo "Your file does not match our criteria";
			return false;
			}
			
		if( $fileSize > $max )
			{
			echo "File is too big";
			return false;
			}
		
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$date = date("d F, o");
$time = date("g:ia");

	$FileRename="";
	for ($i=0; $i<8; $i++) $FileRename .=rand(1,9);
	
	$path = "images/";
	if(is_uploaded_file($_FILES['userfile']['tmp_name']))
		{
		move_uploaded_file($_FILES['userfile']['tnp_name'], $path);
		}
		else
		{
		echo "file not uploaded";
		return false;
	}
	
	mysql_query("INSERT INTO avatar ('user','filename','date','time') VALUES
	('{$_SESSION['username']['usr_user']}', '$FileRename', '$date', '$time')") or die('Query Failed: '.mysql_error().'');

echo 'Image Uploaded';
	}
	return true;
}


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 23 September 2006 - 08:54 PM

Add this code instead of
$path = "images/";

$file_ext = substr($_POST["userfile"]["name"], strrpos($_POST["userfile"]["name"], '.')+1);

$dir = "images/";
$new_file_name = $username."_av.".$file_ext;

$path = $dir.$new_file_name;

Tell me the problem, I will try tell you the solution

#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 23 September 2006 - 08:56 PM

thx, I sorted it earlier :]
thanks though!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users