Jump to content

Archived

This topic is now archived and is closed to further replies.

xyn

Rename Uploaded file

Recommended Posts

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
[code=php:0]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;
}
[/code]

Share this post


Link to post
Share on other sites
Add this code instead of
$path = "images/";

[code]
$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;
[/code]

Share this post


Link to post
Share on other sites
thx, I sorted it earlier :]
thanks though!

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.