View Single Post
  #6 (permalink)  
Old 04-02-08, 05:44 AM
davestar057 davestar057 is offline
Wannabe Coder
 
Join Date: Dec 2007
Posts: 115
Thanks: 1
Thanked 1 Time in 1 Post
PHP Code:

if (isset($_POST['submitted'])) {


    
// Check for an uploaded file.
    
if (isset($_FILES['upload'])) {
        
        
// Validate the type. Should be jpeg, jpg, or gif.
        
$allowed = array ('image/gif''image/jpeg''image/jpg''image/pjpeg');
        if (
in_array($_FILES['upload']['type'], $allowed)) {
        
            
// Move the file over.
            
if (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/{$_FILES['upload']['name']}")) {
            
                echo 
'<p>The file has been uploaded!</p>';
            
            } else { 
// Couldn't move the file over.
            
                
echo '<p><font color="red">The file could not be uploaded because: </b>';
        
                
// Print a message based upon the error.
                
switch ($_FILES['upload']['error']) {
                    case 
1:
                        print 
'The file exceeds the upload_max_filesize setting in php.ini.';
                        break;
                    case 
2:
                        print 
'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
                        break;
                    case 
3:
                        print 
'The file was only partially uploaded.';
                        break;
                    case 
4:
                        print 
'No file was uploaded.';
                        break;
                    case 
6:
                        print 
'No temporary folder was available.';
                        break;
                    default:
                        print 
'A system error occurred.';
                        break;
                } 
// End of switch.
                
                
print '</b></font></p>';

            } 
// End of move... IF.
            
        
} else { // Invalid type.
            
echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
            
unlink ($_FILES['upload']['tmp_name']); // Delete the file.
        
}

    } else { 
// No file uploaded.
        
echo '<p><font color="red">Please upload a JPEG or GIF image smaller than 512KB.</font></p>';
    }
            
// End of the submitted conditional.
?>
    
<form enctype="multipart/form-data" action="upload_image.php" method="post">

    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
    
    <fieldset><legend>Select a JPEG or GIF image to be uploaded:</legend>
    
    <p><b>File:</b> <input type="file" name="upload" /></p>
    
    </fieldset>
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>
    <input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html> 
Reply With Quote