Hello all,
I've been working on a registration script, and one field of the script is for an 80x80 .gif or .jpg avatar (field name is "avatar"). While the rest of my registration script works just fine, I can't seem to get the avatar upload to work properly.
Here's how it should work...
-Users can decide whether or not to upload an avatar
-If left blank, $avatar should be set to "noavatar.gif"
-If a user selects an avatar, it should upload to the server AS LONG AS THERE IS NO FILE WITH THE SAME FILENAME
(if there is one with the same filename, it should rename the new one before uploading, I don't know how to do this part so right now it forces the user to rename before uploading)
Here is my code:
PHP Code:
if ($_FILES['avatar']) { $uploaddir = "uploads/avatars/"; $allowed_ext = "jpg, gif, png"; $max_height = "80"; $max_width = "80"; $extension = pathinfo($_FILES['avatar']['name']); $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } list($width, $height, $type, $w) = getimagesize($_FILES['avatar']['tmp_name']); if($width > $max_width || $height > $max_height) { echo "<div align='center'><span class='error'>Sorry, but your avatar is too big.</span></div>"; include "lib/myadventure/regform.php"; } else { $filename = "uploads/avatars/" . $_FILES['avatar']['name']; if (file_exists($filename)) { echo "<div align='center'><span class='error'>Sorry, but you will need to rename your avatar and try again.</span></div>"; include "lib/myadventure/regform.php"; } else { if(is_uploaded_file($_FILES['avatar']['tmp_name'])) { move_uploaded_file($_FILES['avatar']['tmp_name'],$uploaddir.'/'.$_FILES['avatar']['name']); $avatar = $_FILES['avatar']['name']; } } } } else { $avatar = "noavatar.gif"; }
What's happening is that when a user uploads an avatar with a unique filename, everything works correctly. When a user DOESN'T upload an avatar, it gives the "you need to rename your avatar" error.
I'm kinda new to uploading via PHP, so any help would be GREATLY appreciated!
Thanks,
-Jon "Reality15" Roost
Last edited by Reality15 : 11-04-2007 at 04:02 PM.
|