Jump to content

Error reporting within form


jason360

Recommended Posts

Hey guys...I have another question.  I am trying to get error reporting to echo out in my form from my upload script.  I followed an example from the forums and tried a few different things with no luck.  Any help with what I am doing wrong would be highly appreciated!

 

Thanks again!

 

JK

 

This is my form code where errors are to be displayed:

<?php 

include("image_upload_script.php");




?>
        
<form enctype="multipart/form-data" class="clearfix" action="" method="post">
						<?php
                            
                            if( isset( $_POST['Upload It'] ) ) {
								$fileName = $_POST['Upload It'];
							} else {
								$fileName = '';
							}
                        ?>
Choose your file here:
<input name="uploaded_file" type="file"/><br /><br />
<input type="submit" name="submit" value="Upload It" class="" />
</form>

This is my simplified upload script with the error messages:

session_start();
$pid ='1000';
if($_POST['submit']=='Upload It')
{

// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$fileName = $pid.".".$fileExt; //rename


// START PHP Image Upload Error Handling --------------------------------------------------
$errormessage =  array();
if (!$fileTmpLoc) { // if file not chosen
   $errormessage[] = 'ERROR: Please browse for a file before clicking the upload button.';
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
	$errormessage[] = 'ERROR: Your file was larger than 5 Megabytes in size.';
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
}


?>
Link to comment
Share on other sites

Hi, Guy!  This is pretty simple.

 

1. The error message are being stored in $errormessage, however this is not defined until the form is submitted.  We'll need to define the error array even when the form as not been submitted, albeit, empty.

2. Now that we define an array to hold any errors, we can check it when ever!  Since we can check it whenever.... it makes the form reusable (on initial display, on error display)

 

I have not tested this code.

<?php    session_start();    # start session
    $pid = 1000;        # whatever this is
    
    $submit = false;          # form has not been submitted ( even so, we don't know yet until we check )
    $errormessage = array();  # need something to hold errors
    
    
    if(isset($_POST['submit'])){  # check if form was submitted by means of clicking the submit button...  
                                  #since we only have 1 submit button, the value of the button 'Upload It'  is moot!
            $submit = true;  # yes, the form as been submitted
            
            if( ! isset($_FILES['uploaded_file']) ){
                $errormessage[] = 'Yikes! My HTML skills suck, and I have no clue!';
            }else{
                if(($errId = $_FILES['uploaded_file']['error'])){
                    # oops, there was an error!  $upload_errors is an array of php upload error codes and messages
                    $upload_errors = array( 
                        0=>'There is no error, the file uploaded with success', 
                        1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini', 
                        2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
                        3=>'The uploaded file was only partially uploaded', 
                        4=>'No file was uploaded', 
                        6=>'Missing a temporary folder' 
                    );
                    $errormessage[] = $upload_errors[$errId];   # get store the php error message in our $errormessage array
                }else{
                    $fileName = $_files['uploaded_file']['name'];
                    $ext = substr($fileName, strrpos($fileName, '.'));
                    if( ! move_uploaded_file($_FILES['uploaded_file']['tmp_name'], '/path/to/perm_file_location/'.$pid.$ext) ){
                        # error storing the uploaded file permanently!
                        $errormessage[] = 'Could not permanently store the uploaded file! Perhaps a permissions issue';
                    }
                }
            }
    }
    
    # if form not submitted, or form submission has errors
    if( ! $submit || $errormessage ){
        # print the form to browser
            # foreach error message, print the message. if there are none.... none will print!
            foreach($errormessage as $msg){ 
                echo "<div style='color:red'>{$msg}</div>";
            }
        ?>


        <form enctype="multipart/form-data" class="clearfix" action="" method="post">
        Choose your file here:
        <input name="uploaded_file" type="file"/><br /><br />
        <input type="submit" name="submit" value="Upload It" class="" />
        </form>
        <?php
        
    }else{  # submitted, and no errors!!
        echo '<div style="color:green">Nom Nom Nom! We got your file and it was delicious! Send more, Thanks!</div>';
    }
Edited by objnoob
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.