Page 1 of 1

Renaming a file being uploaded

Posted: Sun Aug 24, 2003 10:43 am
by Aaron

Code: Select all

# --
	function upload($the_file) 
	{global $the_path,$the_file_name;
	$error = validate_upload($the_file);
	if($error)
	{form($error);} else 
	{if (!@copy($the_file, $the_path . "/" . $the_file_name))
	{$error = "Something went wrong, avatar wasnt uploaded.";
	$forward = "yes";
	error_box($error, $forward);}
	else
	{$success = "Your avatar has been uploaded.";
	$forward = "index.php?section=edit_account";
	success_box($success, $forward);}}}
	# END upload
Thats the upload process (with a few other functions to clean the size and height/width) and I was wondering how to make the file name change into the usersID ($user_properties['uid']) after or before its uploaded...any help?

Posted: Sun Aug 24, 2003 1:10 pm
by delorian
Change the $the_file_name variable to usersID.

Posted: Sun Aug 24, 2003 2:14 pm
by Aaron
Wouldnt that remove the .gif and .jpg...

Posted: Sun Aug 24, 2003 2:37 pm
by Aaron
I exploded it out and stuff and I still cant get it to work O_o

Code: Select all

function upload($the_file) 
   {global $the_path,$the_file_name; 
   $error = validate_upload($the_file); 
   if($error) 
   {form($error);} else 
   
   {$the_file1 = explode(".", $the_file_name);
	$the_file_name = "".$user_properties['uid'].".".$the_file1[1]."";
	
   if (!@copy($the_file, $the_path . "/" . $the_file_name)) 
   {$error = "Something went wrong, avatar wasnt uploaded."; 
   $forward = "yes"; 
   error_box($error, $forward);} 
   else 
   {$success = "Your avatar has been uploaded."; 
  // $forward = "index.php?section=edit_account"; 
   success_box($success, $forward);}}}

Posted: Sun Aug 24, 2003 3:22 pm
by volka
what about

Code: Select all

$the_fileextension = strrchr('.', $the_file_name);
$the_file_name = $user_properties['uid'].$the_fileextension;
?

as a sidenote: I sugest some indents for your code. It's much more readable then.

Posted: Sun Aug 24, 2003 3:31 pm
by Aaron
Ive just re-wrote it totally, functions are to extreme for something really simple like this...here it goes

Code: Select all

<?

	if($uploadsubmit) 
	{$avsize = getimagesize($avatar);
	if(!is_uploaded_file($avatar))
	
	{$error = "Are you trying to hurt me? *logged*.";
	$forward = "yes";
	error_box($error, $forward);}

	elseif($avatar_size > 20480)
	{$error = "Your file is too big. max size is 20 KB.";
	$forward = "yes";
	error_box($error, $forward);}

	elseif($avatar_type != "image/gif" && $avatar_type != "image/pjpeg")
	{$error = "You can only upload a GIF or JPEG image.";
	$forward = "yes";
	error_box($error, $forward);}

	elseif($avsize[0] > 60 || $avsize[1] > 60)
	{$error = "Your avatar is too big. it must be under 60x60.";
	$forward = "yes";
	error_box($error, $forward);}

	else
	{$explode = explode(".", $avatar_name);
	$extension = $explode[1];

	$success = "Your avatar has been uploaded.";
	$forward = "index.php";
	success_box($success, $forward);
	
	$uid = "".$user_properties['uid']."";
	copy($avatar, "images/wuggyuploads/$uid.$extension");
	unlink($avatar);}}

		else

	{echo "	<table width='100%' cellspacing='1' cellpadding='4' class='bg6'  style='margin-top : 7px;'>
<tr align='left' valign='top'> 
<td class='bg6'> <p><font class='newstitle'>Uploading avatar</font><br>  
<tr align='left' valign='top'> 
<td  class='bg3'>
<form action='?section=test' method='post' enctype='multipart/form-data'>
<input type='file' name='avatar'>
<input type='submit' name='uploadsubmit' value='Upload'>NOTE: this will overwrite the previous avatar you uploaded. 
</td></tr></table></form>";}

?>
works perfectly and its straight forward

Posted: Sun Aug 24, 2003 4:12 pm
by phice
Wow, that's a weird way of handling your brackets ({ and }). :P

Posted: Sun Aug 24, 2003 4:45 pm
by volka
two notes on your new code, Aaron.
  • $avatar_[name|size|type] only work with register_globals enableded (or old versions). In more recent versions the information is stored in the superglobal $_FILES
  • what do you do, if the code contains more or less than one dot? Since you alread rely on the mime type of the uploaded image, why not use it to determine the proper extension?
    btw the mime type is only courtesy of the client. It's not trustworthy. If you have access to either getimagesize() or exif_imagetype() think about using them

Posted: Mon Aug 25, 2003 7:50 am
by Aaron
phice wrote:Wow, that's a weird way of handling your brackets ({ and }). :P
so everyone keeps telling me, its so easy to understand for me, I dont understand other peoples scripts tbh...

volka @ Thanks for the input, I dont understand the fuss with superglobals, I wouldnt know how to impliment it into my code, plus my codes working so Im happy.

I also tried using exif_imagetype() but it seems my server doesnt support it

Fatal error: Call to undefined function: exif_imagetype() in /home/azz0r/public_html/pages/member/edit/editing.php on line 59

ANYWAY. Heres the current code.

Code: Select all

<?

$bday = $year.'-'.$month.'-'.$date;

	if(!$username || !$email)
	{$error = "You missed something out (username or email address).";
	$forward = "yes";
	error_box($error, $forward);} 

	else if (!eregi("^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?$", $email)) 
	{$error = "Your email address seemed incorrect.";
	$forward = "yes";
	error_box($error, $forward);} 

	else if($site && !strstr($site, "http://"))
	{$error = "Your site url is invalid (http:// remember).";
	$forward = "yes";
	error_box($error, $forward);}
	
	else if ($msn && !eregi("^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?$", $msn)) 
	{$error = "Your MSN email address seemed incorrect.";
	$forward = "yes";
	error_box($error, $forward);} 
	
	else if (!$sex)
	{$error = "Please select a sex, if you do not wish to give such information please select N/A.";
	$forward = "yes";
	error_box($error, $forward);}
	
	else if (strlen($tag) > 2000)
	{$error = "You dont want to say to much, hence your FreeSpeech box is limited to 2000 letters.";
	$forward = "yes";
	error_box($error, $forward);}

	else if (strlen($sig) > 500)
	{$error = "Signatures are limited to 500 letters.";
	$forward = "yes";
	error_box($error, $forward);}
	
	else
	
	{//IF FORM SUBMITTED THE AVATAR THEN CONTINUE TO CHECK THE FILE
	if($uploadsubmit) 
	{$avsize = getimagesize($avatar);
	
//IF USER IS UNLINKING THE FILE THEN WARN THEM (FAKE LOG THREAT)
	if(!is_uploaded_file($avatar))
	{$error = "Are you trying to hurt me? *logged*.";
	$forward = "yes";
	error_box($error, $forward);}

//IF AVATAR SIZE IS OVER 20KB WARN THE USER
	elseif($avatar_size > 20480)
	{$error = "Your file is too big. max size is 20 KB.";
	$forward = "yes";
	error_box($error, $forward);}

// IF THE FILE ISNT A JPG OR GIF THEN WARN THEM
	elseif($avatar_type != "image/gif" && $avatar_type != "image/pjpeg")
	{$error = "You can only upload a GIF or JPEG image.";
	$forward = "yes";
	error_box($error, $forward);}

//IF AVATAR IS BIGGER THAN 50 BY 50 WARN THE USER
	elseif($avsize[0] > 50 || $avsize[1] > 50)
	{$error = "Your avatar is too big, it must be under 50x50.";
	$forward = "yes";
	error_box($error, $forward);}

	else
	
	//EXPLODE THE AVATAR NAME
	{$explode = explode(".", $avatar_name);
	$extension = $explode[1];

//ADDS THE USERID TO THE EXTENSION AND COPYS IT TO THE SERVER AND UNLINKS THE AVATAR TO CLEAR THE CACHE	
	$uid = "".$user_properties['uid']."";
	copy($avatar, "images/wuggyuploads/$uid.$extension");
	unlink($avatar);
	
	mysql_query("UPDATE unz_users SET email ='$email', email_hide ='$email_hide', avatar = '$extension', site = '$site', msn ='$msn', msn_hide ='$msn_hide', sex = '$sex', bday ='$bday', sig ='$sig', tag ='$tag', ip ='$ip', host = '$host' WHERE uid = ".$user_properties['uid']."");
	$success = "Changes applied to your account.";
	$forward = "index.php";
	success_box($success, $forward);}}
	
	else
	
	{$error = "Some sort of problem...";
	$forward = "yes";
	error_box($error, $forward);}}

?>
The current problem Im having is an overwrite issue, Ive set the folder to 777 but I cant upload over the older image O_o even with my ftp folder, quite bizare.

Code: Select all

Warning: copy(images/wuggyuploads/1.gif): failed to open stream: Permission denied in /home/azz0r/public_html/pages/member/edit/editing.php on line 78

Success
Changes applied to your account.