Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 69
Trades: 0
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
 
 
Register now for full access!
Old 11-04-2007, 04:20 PM Re: Help uploading image in script
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
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.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.12358 seconds with 12 queries