Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-16-2012, 01:55 AM   PM User | #1
Nikey646
New Coder

 
Join Date: May 2012
Posts: 31
Thanks: 0
Thanked 7 Times in 7 Posts
Nikey646 is an unknown quantity at this point
Upload Error Processing

This is only the Error Processing portion of an uploading script.

It includes
"No File Selected"
"Unsupported File Type"
"File size is above limit"
Type errors.

It uses an Array for allowed file extensions, checks the extension using pathinfo on the upload file, Size is set by Megabytes (Then calculated into bytes :P)

all errors are in an Array, called "Errors" which is echoed out in an Unordered List at the very bottom of the page.

Some Commented code for Display All allowed files.

uses the variable "Processed" to check weather or not the file clears all required attrubites.

Minium Comments explaining some stuff.

HTML
Code:
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">

    File:
    <input type="file" name="file_uploader">
    <input type="submit" value="Upload">

</form>
</body>
</html>
PHP (Place Above/Below HTML, Your choice, Both Work.)
PHP Code:
     <?php
    
//Defining some Variables for Error Checking.
    
    //Set File Size
    
$max "1";
    
$max = (($max 1024)*1024);

    
//Allowed File Types
    
$extensions = array("dem""png""jpg");

    
//Do not Edit Belo this Comment (:
    //Grabbing File Extension
    
$file $_FILES['file_uploader']['name'];
    
$ext pathinfo($filePATHINFO_EXTENSION);

    
//Other Stuffs :D    
    
$size $_FILES['file_uploader']['size'];
    
$processed false;
    
$errors[] = false;
    unset(
$errors[0]);
    
    
//Some Basic Error Checking

    
if(!isset($_FILES['file_uploader']['tmp_name'])){
        
$errors[] = "No File Selected";
    } else {
        if(!
in_array($ext$extensions)){
            
$errors[] = "Unsupported File Type.";
        } else {
            if(
$size $max){
                
$max = (($max/1024)/1024);
                
$errors[] = "File size is to large, max file size is {$max}Mb(s)";
            } else {
                
$processed true;
            }
        }
    }

    
//If nothing came back as an error, continue with basic file processing and
    //Any MySQL Database information.

    
if($processed){
        echo 
"No Errors. Please Continue.";
    }

    
//Process all and any Errors.
    
if($errors){
        echo 
"<ul>";
        foreach(
$errors as $error){
            echo 
"<li>{$error}</li>";
        }
        echo 
"</ul>";
    }

    
//Testing Section, all Tests will be commented in the code for your enjoyment

    //Code to display all avaiable Extensions(Cleanly echo out the $extensions
    //Array, with a . before all exts.)
    // for($i=0; $i<count($extensions); $i++){
    //     echo '.'.$extensions[$i].' ';
    // }
?>
If anyone wants to create the processing portion of this, go ahead (:

If theres a part that you dont understand, post a comment, i'll get back ASAP.
Nikey646 is offline   Reply With Quote
Reply

Bookmarks

Tags
error processing, file extension limits, upload script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 12:27 AM.


Advertisement
Log in to turn off these ads.