警报不起作用

时间:2017-07-26 13:22:32

标签: php html file upload alert

如果用户没有在input元素中上传文件,我试图在php文件中弹出警告。

index.php中的源代码:

<input type='file' name="imgInp" id="imgInp" accept=".jpg, .png, .jpeg|images/*"/>
<img style="max-height: 400px; max-width: 400px;" id="blah" src="#" alt="your image" />

在提交的php文件中:

if($_FILES["imgInp"]["name"]!='')
{
$first_file=rand().$_FILES["imgInp"]["name"];
$path="images/template/usersupload/";
$tot_dir=$path.$first_file;
    if(file_exists($path))
    {
        move_uploaded_file($_FILES["imgInp"]["tmp_name"],$tot_dir);
    }
}
else
{
    echo '<script>alert("You forgot to upload a file!!!");
                                             </script>';
                                              echo'<script type="text/javascript">';
                                             ?>
               window.location="index.php";
          <?php
          echo '</script>';

}

1 个答案:

答案 0 :(得分:-1)

更改您提交Php代码至 你忘了在脚本标签中包裹警报:)

        if($_FILES["imgInp"]["name"]!='')
        {
        $first_file=rand().$_FILES["imgInp"]["name"];
        $path="images/template/usersupload/";
        $tot_dir=$path.$first_file;
            if(file_exists($path))
            {
                move_uploaded_file($_FILES["imgInp"]["tmp_name"],$tot_dir);
            }
        }
        else
        {
        echo'<script type="text/javascript">';
            echo '<script>alert("You forgot to upload a file!!!");
    setInterval(function(){window.location.href="index.php" },5000);
    </script>';
                                                        echo '</script>';
 }
                                                     ?>