View Single Post
Old 08-10-2010, 10:58 PM   #3 (permalink)
sjaq
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

PHP Code:
<?php
    define 
("MAX_SIZE","1024"); 

    
$errors=0;
    if(isset(
$_POST['Submit'])) {
        
$image=$_FILES['image']['name'];
        if(
$image) {
            
$filename stripslashes($_FILES['image']['name']);
            
$extension strtolower(end(explode('.'$filename)));
            if (
current(explode('/'$_FILES['image']['type'])) != 'image') {
                echo 
'<h1>Unknown extension!</h1>';
                
$errors=1;
            } else {
                
$size=$_FILES['image']['size'];

                if (
$size MAX_SIZE*1024) {
                    echo 
'<h1>You have exceeded the size limit!</h1>';
                    
$errors=1;
                }

                
//we will give an unique name, for example the time in unix time format
                
$avatar $_POST['avatar'];
                
//the new name will be containing the full path where will be stored (images folder)
                
if(function_exists('sha1_file')) {
                    
$hash sha1_file($_FILES['image']['tmp_name']);
                } else if(
function_exists('md5_file')) {
                    
$hash md5_file($_FILES['image']['tmp_name']);
                } else { 
$hash $_FILES['image']['tmp_name']; }
                
                
$newname="./forums/images/avatars/gallery/".$avatar."/"$hash '.' $extension;
                
//we verify if the image has been uploaded, and print error instead
                
if(!file_exists($newname)) {
                    if (!
move_uploaded_files($_FILES['image']['tmp_name'], $newname)) {
                        echo 
'<center>Upload unsuccessfull!</center>';
                        
$errors=1;
                    }
                }
            }
        }
    }


    
//If no errors registred, print the success message
     
if(isset($_POST['Submit']) && !$errors) {
         echo 
'<center>Your avatar has been uploaded!<br /><br />

        <img src=../../'
.$newname.'><br>
        To view the Avatar Gallery go to "<a href="/forums/profile.php?mode=editprofile">Edit Profile</a>" go to the Avatar section and click "Show Gallery"!
        <a href="/avatar/upload/">Upload</a> another avatar. 
        </center>'
;
    } else {
        include 
'form.php';
    }
    
?>
sjaq is offline  
Reply With Quote