0

我有这个。只是想知道exit;是否有另一种方法可以在回声之后停止正在执行的函数?

在我的情况下,我不能使用的原因exit;是,如果我这样做,回显发生在不同的窗口中,我需要点击浏览器的后退按钮才能返回原始页面。但是,如果我删除exit;消息打印在同一页面上,但不是特定的,而是数组中的所有消息都按顺序打印在屏幕上。

那么你能提出什么解决方案呢?

    <?php
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$max_file_size = 3500000; // size in bytes

if(isset($_POST['submitForm']))
{
    if(!isset($_POST['chkOne']))
    {$ErrMsg = 'Tick the check box if you wish to upload images!';} 
    else{
    /*-------START------- */

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index.php';
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';

$fieldname = 'file';

    function error($error, $location, $seconds = 5)
    {

        echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
        '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
        '<html lang="en">'."\n".
        '   <head>'."\n".
        '       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
        '       <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
        '   <title>Upload error</title>'."\n\n".
        '   </head>'."\n\n".
        '   <body>'."\n\n".
        '   <div id="Upload">'."\n\n".
        '       <h1>Upload failure</h1>'."\n\n".
        '       <p>An error has occured: '."\n\n".
        '       <span class="red">' . $error . '...</span>'."\n\n".
        '       The upload form is reloading</p>'."\n\n".
        '    </div>'."\n\n".
        '</html>';

    } 
    $errors = array(1 => 'php.ini max file size exceeded', 
                    2 => 'html form max file size exceeded', 
                    3 => 'file upload was only partial', 
                    4 => 'no file was attached');

// check the upload form was actually submitted else print form
isset($_POST['submitForm'])
    or error('the upload form is neaded', $uploadForm);

// some code //

$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
    $now++;
}
$ErrMsg = "<b>Congratulations! Your file upload was successful</b>";

    /*-------END------- */
        }
}

?>
<!doctype html>
<html>
<head>
</head>

<body>
       <form id="Upload" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
           <table name="loadertable" width="394" border="1">
            <td>Upload image :</td>
              <td>
                    <div id="upload_title_text"><input type="checkbox" name="chkOne" id="chkOne">Upload these images:</div>
                    <div id="FileUploadDiv" DivValue="0">
                    <input id="file" type="file" name="file">
                    </div>
                    <input type="hidden" value="0" id="counter" name="counter">
                    <div align='left'></div>
        <p>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
        </p>
              </td>
              <tr>
              <td>&nbsp;</td>
              <td><input id="submitForm" type="submit" name="submitForm" value="Preview"></td>
              </tr>
            <tr>
             <td><?php if(!empty($ErrMsg)) echo $ErrMsg; ?></td>
            </tr>
          </table>

        </form>

</body>
</html>
4

1 回答 1

0

你可以试试returning。引用PHP 文档

如果从函数内调用,该return语句将立即结束当前函数的执行

所以,基本上,而不是exit;使用return false;.

根据具体情况,抛出异常可能会更好。

于 2013-08-30T17:29:07.137 回答