xyn Posted September 23, 2006 Share Posted September 23, 2006 Hey guys,I've changed my format from my last file uploading scriptstill uploading an avatar, but i've decided to store theimage inside a folder called "images" and I wanted torename the image.My problem, I'm not sure how to rename an uploaded imagebut 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 tothe 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] Link to comment Share on other sites More sharing options...
onlyican Posted September 23, 2006 Share Posted September 23, 2006 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] Link to comment Share on other sites More sharing options...
xyn Posted September 23, 2006 Author Share Posted September 23, 2006 thx, I sorted it earlier :]thanks though! Link to comment Share on other sites More sharing options...
Recommended Posts