tags:

views:

119

answers:

4

I have an image upload page (single page). When submitted it check filesize and type, exiting on each if they violate the rules. Problem is the entire page stops.

How can i stop the remainder of the php (or break out of current <? ?>) from being processed and just load the rest of the page?

+6  A: 

You should move your code into a function, then you can just return from it.

function processImage($img)
{
    if (imageIsTooLarge($img))
        return false;

    doOtherStuff();
    return true;
}

$ok = processImage($someImage);
Greg
+1  A: 

You could, for simplicities sake, wrap the code in an if() statement.

// continue parsing image if filesize not greater than maxsize
if ($filesize <= $max_size) {

    // contine parsing image if filetype is fine
    if (in_array($extension, array('jpg','jpeg','gif')) {

        // remainder of your PHP code goes in here for parsing image upload

    }
}

All your HTML should be below this block of PHP.

cballou
+1  A: 

By exiting do you literally mean exit()? If so consider generating an error message for the user and displaying that, you can just discard the file that is uploaded.

if(image is too large) {
    $err = "The image you uploaded is too large";
}

if(image wrong file type) {
    $err = "You have not uploaded a valid image file";
}

if(!isset($err) {
    proceesImage();
}

// echo out $err to user
RMcLeod
+3  A: 

if you don't want to pollute the namespace with extra functions and whatnot, this is the perfect situation for a do { ... } while (0); loop.

do {
    // processing
    if (!check_file_size($image)) {
        echo 'The image is too big';
        break;
    }

    if (!check_file_type($image)) {
        echo 'The image is of the wrong type';
        break;
    }

    echo $image;
} while (0);

The do-while(0) loop is the unsung hero of getting conditional exits from some sort of processing without having to write a function and function call into your code. While the gains will be negligible, this will also prevent the PHP parser from having to create an extra symbol and then look it up again for little-to-no reason.

EDIT: it also prevents you from getting into gigantic if-block pyramids when your conditionals get too large. If you wrap it in an if block, and each subsequent condition inside a dependant if-block, you eventually have this giant, hard-to-follow mess of indentation (assuming you format your code) with closing braces that can be difficult to trace to their opening blocks; using do { ... } while (0); keeps everything at the same logical indentation level.

Dereleased
Thanks for the info. I went with this suggestion, but thanks to everyone for their input. Always love to learn something new.
Patrick
+1, I love this 'trick'. Never seen it used before, so thanks for teaching me something new. :P
Duroth
+1, might come in handy. It might be good to also mix it with @RM's solution to concat the error message if you're looking for a more verbose error message (i.e. *filesize too large* **AND** *invalid extension*).
cballou