Reply
Help uploading image in script
Old 11-04-2007, 04:01 PM Help uploading image in script
Skilled Talker

Posts: 69
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.
Reality15 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 11-04-2007, 04:20 PM Re: Help uploading image in script
rogem002's Avatar
Webmaster Talker

Posts: 612
Name: Mike
Location: United Kingdom
Well, thats due to an image already being there. I recommend you put a timestamp in.

PHP Code:
$timestamp md5(mircotime()); // I added this line. In a nutshell, an encrypted version of the time.
            
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/" $timestamp."_".$_FILES['avatar']['name']; // Edited this line
                
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.'/'.$timestamp."_".$_FILES['avatar']['name'].); // Edited this line.
                
                
$avatar $timestamp."_".$_FILES['avatar']['name']; // And this line
                
}
                }
                }
            } else {            
                
$avatar "noavatar.gif";
            } 
This way, the names are unique.
__________________
PHP Code:
Add_Talkupation('rogem002'); // Because sharing is awesome! 

Last edited by rogem002 : 11-04-2007 at 04:23 PM.
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Reply     « Reply to Help uploading image in script
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11880 seconds with 12 queries