Page 1 of 1

something like Goto?

Posted: Tue Apr 22, 2008 1:50 pm
by haosmark

Code: Select all

<?php
if (!isset($_POST['upload']))
    exit();
    
if ($_FILES['pix']['tmp_name'] == "none") {
    echo "<p style = 'font-weight: bold;'>File didn't successfully upload. Check he file size. File must be less than 500K.</p>";
    exit();
}
if (!preg_match("/image/", $_FILES['pix']['type'])) {
    echo "<p style = 'font-weight: bold';>The file has to be an image.";
    exit();
}
 
$destination = 'C:\data' . "\\" . $_FILES['pix']['name'];
$temp_file = $_FILES['pix']['tmp_name'];
move_uploaded_file($temp_file, $destination);
echo "<p style = 'font-weight: bold';>The file has been successfully uploaded: {$_FILES['pix']['name']} - {$_FILES['pix']['size']}</p>";
?>
<html>
    <head>
        <title>form_with_upload</title>
    </head>
    <body>
    <ol>
        <li>Enter the file name</li>
        <li>Click Upload Picture</li>
    </ol>
    <div style = 'text-align: center'><hr />
    <form enctype = "multipart/form-data" action = "<?php $_SERVER['PHP_SELF']; ?>" method = "post">
        <P><input type = "hidden" name = "MAX_FILE_SIZE" value = "500000" />
           <input type = "file" name = "pix" size = "60" /></p>
           <input type = "hidden" name = "submitted" value = "1" />
           <input type = "submit" name = "upload" value = "Upload Picture">
    </form>
    </div>
    </body>
</html>
How do i return to displaying a form without using includes? I though that exit() will do the trick, but apparently i was wrong.

Re: something like Goto?

Posted: Tue Apr 22, 2008 2:40 pm
by andym01480
exit(); stops the resy of the whole script being parsed including the html form at the bottom. Do it like this...

Code: Select all

if(!isset($_POST['submitted']))//use the hidden field rather than the submit button, because of IE bug when enter is pressed
{
//display form
 
exit();
}
process form

Re: something like Goto?

Posted: Tue Apr 22, 2008 3:37 pm
by haosmark
Thanks, but this works only for the first time the form is displayed, how can i go back to the form without using include, for example is this condition is met: if (!isset($_POST['upload']))
?