上传文件PHP时显示错误

时间:2018-09-12 15:15:45

标签: javascript php html5

我正在制作一个用户个人资料编辑页面,并且我添加了个人资料图片上传选项,但是当文件大小超出限制时它不会显示错误消息

if(isset($_POST["submits"]))
{
$target_dir = "uploads/";
$target_maxsize=2000000;
if(!($_FILES['fileToUpload']['name']==""))
{
    $target_file = $target_dir.basename($_FILES['fileToUpload']['name']);
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") 
    {
        $_SESSION['uperr']="Please select an image file (.jpg, .jpeg & .png)";
        header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
    }
    else
    {
        if($_FILES['fileToUpload']['size'] < $target_maxsize)
        {
            if(file_exists($target_file))
            {
                $_SESSION['uperr']="File Already Exists!";
                header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
            }
            else
            {
                if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file))
                {
                $_SESSION['uperr']="File has been uploaded successfully!";
                header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
                }
                else
                {
                    $_SESSION['uperr']="File could not be uploaded!";
                    header('Location: http://localhost/VULCAN/dashboard.php?q=setting');    
                }
            }
        }
        else
        {
            $_SESSION['uperr']="File is too large!";
            header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
        }
    }
}
else
{
    $_SESSION['uperr']="Please select a file first!";
    header('Location: http://localhost/VULCAN/dashboard.php?q=setting');
}
}

这是我用来显示错误消息的内容

<body>
<?php
if(isset($_SESSION['uperr'])/*$sesfilerr!=""*/)
{
    ?><script>
        $( document ).ready(function() {
            alert("<?php echo $_SESSION['uperr']; ?>");
        });
        </script><?php
unset($_SESSION['uperr']);
}
?>
</body>

帮助任何人吗? 另外,当我刷新页面时,错误消息会再次显示。有什么预防方法吗?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码

group_pct