ben
ben

Reputation: 29

PHP code breaking HTML layout

I have a simple file uploader, which thanks to stackoverflow is now fully working, however when I copied the PHP code across to my main layout, once initialised to upload a file, but it is wrong format or size and it echos the error, it breaks the HTML below it. Im thinking its to do with the "exit;" after each echo? but could be wrong.

<?php
if($_POST['upload']) {
if($_FILES['image']['name'] == "")
{
    #there's no file name return an error
    echo "<br/><b>Please select a file to upload!\n</b>";
    exit;
}
#we have a filename, continue

#directory to upload to
$uploads = '/home/habbonow/public_html/other/quacked/photos';
$usruploads = 'photos';

#allowed file types
$type_array = array(image_type_to_mime_type(IMAGETYPE_JPEG), image_type_to_mime_type(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_PNG), 'image/pjpeg');

if(!in_array($_FILES['image']['type'], $type_array))
{
    #the type of the file is not in the list we want to allow
    echo "<br/><b>That file type is not allowed!\n</b>";
    exit;
}

$max_filesize = 512000;
$max_filesize_kb = ($max_filesize / 1024);

if($_FILES['image']['size'] > $max_filesize)
{
    #file is larger than the value of $max_filesize return an error
    echo "<br/><b>Your file is too large, files may be up to ".$max_filesize_kb."kb\n</b>";
    exit;
}

$imagesize = getimagesize($_FILES['image']['tmp_name']);

#get width
$imagewidth = $imagesize[0];
#get height
$imageheight = $imagesize[1];

#allowed dimensions
$maxwidth = 1024;
$maxheight = 1024;

if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
    #one or both of the image dimensions are larger than the allowed sizes return an error
    echo "<br/><b>Your file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n</b>";
    exit;
}

move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']) or die ("Couldn't upload ".$_FILES['image']['name']." \n");

echo "<br/>The URL to your photo is <b>" . $usruploads . "/" . $_FILES['image']['name'] . "</b>. Please use this when defining the gallery photos";

}

?>

<form name="uploader" method="post" action="" enctype="multipart/form-data">
      <input type="file" name="image" style="width:300px;cursor:pointer" />
      <input type="submit" name="upload" value="Upload Image" />
</form>

Upvotes: 1

Views: 1175

Answers (3)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

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

OR

if(!empty($_POST['upload'])){

And remove exit...

Upvotes: 1

slhck
slhck

Reputation: 38652

If you call exit; your PHP script won't be able to output anything anymore. That's why the layout is broken.

You should maybe try to keep the HTML parts out of your PHP code and especially avoid opening tags that you don't close afterwards (i.e. divs or anything).

That being said, it's probably safest to just put everything into a function that won't exit the script when finished (see other's posts).

Upvotes: 1

VoteyDisciple
VoteyDisciple

Reputation: 37803

Indeed, when you call exit; it means "immediately stop all processing; this script is finished." Anything that comes after it — including HTML — will not be interpreted.

A better organization would be to make this code a function, to the effect of:

function uploadMyStuffPlease() {

    if($_POST['upload']) {
        if($_FILES['image']['name'] == "")
        {
            #there's no file name return an error
            echo "<br/><b>Please select a file to upload!\n</b>";
            return;
        }
        #we have a filename, continue

    // ....

}

Now you can simply call uploadMyStuffPlease(), which will do as much processing as it can, and perhaps return early in the event of an error. Either way, the function will return, and so the rest of your script (including that HTML) can still be interpreted.

Upvotes: 3

Related Questions