在同一屏幕上打印验证消息,而无需在PHP中重定向

时间:2013-08-25 12:50:03

标签: php

嗨,这是一个新手,非常感谢你的帮助。我从在线教程中借用了这个,并尝试根据我的需求进行定制。

此工作文件但输出消息在另一个页面/屏幕上打印。简单地说我需要的是验证消息将被打印在同一页面/屏幕上而不重定向。我尝试从函数中删除$location但似乎没有帮助我。如何使所有验证消息显示在MSGPane

感谢您的帮助。

这是代码;

<?php
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
if(isset($_POST['submit']))
{
$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 . $_SERVER['PHP_SELF'];
$fieldname = 'file';
$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');

isset($_POST['submit'])
    or error('the upload form is neaded', $uploadForm);

($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);

@getimagesize($_FILES[$fieldname]['tmp_name'])
    or error('only image uploads are allowed', $uploadForm);

$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
    $now++;
}

@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm);

header('Location: ' . $uploadSuccess);

}

function error($error, $location, $seconds = 5)
{
    header("Refresh: $seconds; URL=\"$location\"");

    echo "
    <div id='Upload'>\n\n
    <h1>Upload failure</h1><br>
    <p>An error has occured: 
    <span class='red'>. $error .</span><br>
    The upload form is reloading</p>
    </div>";
    exit;
}
$max_file_size = 3500000; // size in bytes
?>


<!doctype html>
<html>
<head><title></title></head>
<body>
<table>
  <tr>
    <td class='MSGPane'></td>
  </tr>
</table>
</html>

1 个答案:

答案 0 :(得分:0)

您必须从该函数中删除此行header("Refresh: $seconds; URL=\"$location\"");。 此行会将用户重定向到 $ location 中指定的位置。

编辑:同时删除第exit;行。

相关问题